Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
<div class="main" action="then">
   <div class="sub">Rock</div>
   <div class="sub">Austin</div>
</div>
<div class="main" action="now">
  <div class="sub">Cena</div>
  <div class="sub">Bryan</div>
</div>

Jquery:

$('.main').each(function({
    $(this).children('.sub') each({
     //push this.text into associative array
});}))

what i want is array to be something like this to send in php

$arr['then']=>array(rock,austin),
$arr['now']=>array(cena,bryan)

So that i can foreach through each array and get the names.

share|improve this question

closed as unclear what you're asking by Armel Larcier, Damien Pirsy, Joeytje50, Siddharth, Andresch Serj May 26 at 11:01

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
action="then" ? what's that? –  Damien Pirsy May 26 at 10:03
    
Its just a attr nothng to worry about! –  user3508453 May 26 at 10:05
    
You can't define PHP variables in JavaScript (or jQuery for that matter), if that's what you're asking –  Joeytje50 May 26 at 10:28
    
Can you send JSON then parse it in PHP? php.net/manual/en/function.json-decode.php –  Akurn May 26 at 10:37

3 Answers 3

So... something like this?

var result = {};
$(".main").each(function() {
    result[this.getAttribute("action")] = $.map($(".sub",this), function(node) {
        return node.firstChild.nodeValue;
    });
});

Demo on JSFiddle

share|improve this answer

To push this.text into associative array you can use the push method:

var array = [];
array.push(this.text);
share|improve this answer
    
No this will be something like $arr= array(rock,austin,cena,bryan) but i want arrays of specific action containing array of names –  user3508453 May 26 at 10:13
var objToSend = {};
$('.main').each(function({
    var n = $(this).attr('action');
    objToSend[n] = [];
    $(this).children('.sub').each({
         objToSend[n].push($(this).text());
    });
});
var jsonToSend = JSON.stringify(objToSend);

Then post that string jsonToSend to your PHP. To retrieve the string as an array in your PHP, use:

$arr = json_decode($_POST['json'], true);

Where 'json' was the name of the data you sent through the post method (will be whatever name you assigned the jsonToSend string as) and the second argument converts it as a PHP array instead of an object. More information on json_decode here: php.net/manual/en/function.json-decode.php

share|improve this answer
    
Gives error on console cannot push to undefined on line where push is applied –  user3508453 May 26 at 18:55
    
Sorry, I fixed it. Should work now (that error was because objToSend[n] hadn't been defined as an array) –  Akurn May 27 at 6:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.