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.

I know there have been many questions regarding converting PHP arrays to Javascript and the standard answer is along the lines of:

tempary=<?php echo json_encode($tmpary); ?>;

However, when I try to access the array, I can't access the elements. e.g.

document.write(tempary[2]);                                 //debug

even though I know that there is a 3rd element.

Here is the actual code:

global $wpdb;
$query="SELECT * FROM wp_ta_members";
$member_table=$wpdb->get_results($query);
echo "<p>Size of wp_ta_members table is " . count($member_table) . "</p>";      //debug
?>
<script type="text/javascript">
var members = [];
var tempary = [];
</script>
<?php
foreach ($member_table as $member_row)
    {
    $tmpary=array($member_row->member_id,
    ($member_row->current==1)?"current":"not a member",
    $member_row->date_joined,
    $member_row->title,
    $member_row->first_names,
    $member_row->last_names)
    ?>
    <script type="text/javascript">
    tempary=<?php echo json_encode($tmpary); ?>;
    document.write(tempary[2]);                                 //debug
    console.log(tempary);                                   //debug
    members.push(tempary.concat());
</script>
<?php   }
?>

You will notice that I am actually creating an array of arrays which I process later using a loop of:

for (mem in members)
{
document.write(workary.length + " " + members.length + " " + mem.length + " " + members[1] + "<br />");     //debug
document.write(mem[1] + " " + mem[2] + " " + mem[3] + " " + mem[4] +"<br />");      //debug
}

However, all I get for tempary[0] is "1" and for any other elements- "undefined".

If I output tempary itself, I get a string of all the elements separated by commas.

What I am I missing? I want tempary to be a proper JavaScript array.

share|improve this question
    
I would suggest that you put the array values in html elements then call these elements in JS and store it in an array. –  hzjw 23 hours ago
1  
Did you try to console.log(tempary)? What's its results? Besides you wrote $tmpary as a PHP variable. Maybe it's a typo. –  kernel 23 hours ago
    
JSON.parse()? –  Novocaine 22 hours ago
    
'tempary = JSON.parse("<?php echo json_encode($var); ?>");' then console.log(tempary); and see what is the structure if your object in developer mode. –  Finciuc Sergiu 22 hours ago
    
The result of console.log(tempary) is: ["78", "current", "2014-01-01", "Mr", "Fred", "Bloggs"] . That is to say a comma separated list as I said. Yes - $tmpary is a PHP variable. This is the array I want to copy to a JavaScript array. I'll try the JSON.parse idea. –  SgtSunshine 10 hours ago

2 Answers 2

I see that you are overwriting your tempary array on each iteration. And so by the time you json_encode, you are left with just the last item.

Try this instead:

global $wpdb;
$query="SELECT * FROM wp_ta_members";
$member_table=$wpdb->get_results($query);
echo "<p>Size of wp_ta_members table is " . count($member_table) . "</p>";      //debug
?>
<script type="text/javascript">
<?php
$members = array();
foreach ($member_table as $member_row){
    $member = array(
        $member_row->member_id,
        ($member_row->current==1)?"current":"not a member",
        $member_row->date_joined,
        $member_row->title,
        $member_row->first_names,
        $member_row->last_names
    );
    $members[] = $member;
}
?>
var members = <?php echo json_encode($members) ?>;
</script>
<?php   }
?>   
share|improve this answer
    
Thanks for your help. You are right that I am using overwriting tempary on each iteration, but I am also using members.push() to add the contents of tempary each time to members so that I can reuse tempary. I will try your solution however. –  SgtSunshine 11 hours ago
    
Thanks killneel. However, I want to end up with an array of arrays. That is members[] is an array of member[] arrays. The above doesn't seem to do it. –  SgtSunshine 10 hours ago
    
My answer actually builds up a multidimensional array (an array of member arrays) in php and then echo out the json string of that php array. You can see that on each iteration of the foreach loop, $member is initialised and populated with values from the table row. This array is then pushed onto members array so that in the end, members would be a numerically index array with each element in it consisting of an array. –  killneel 1 min ago

Use a foreach loop and write out the JS code on each iteration after declaring the JS array:

<script>

var js_array = new Array();

<?php

    $x = 0;

    foreach($item as $key => $value){

        echo 'js_array[$x] = "' . $value . '";';

        $x++;
    }

?>

</script>
share|improve this answer
    
<?php $x = 0; foreach($member_row as $value){ echo 'tempary[' . $x . '] = "' . $value . '";'; $x++; } ?> –  SgtSunshine 10 hours ago
    
I had to modify your code to: <?php $x = 0; foreach($member_row as $value){ echo 'tempary[' . $x . '] = "' . $value . '";'; $x++; } ?> But it still doesn't work. –  SgtSunshine 10 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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