This code is for a WP site, so I hope it's okay to ask on this site. :)
My PHP code generates an array. This is an example:
$myArray = Array ( [0] => Array ( [0] => United States [1] => 700 Trapp Hill Road [2] => Stowe, Vermont [3] => http://neweddingvenues.com/images/image.png [4] => 350,000 [5] => http://neweddingvenues.com/listing/vermont-venue/ ) )
I need to use this array in Javascript, so I do this to create a JS array variable:
var jsArray = <?php echo json_encode(json_encode($myArray)); ?>;
(it's a nested json_encode because it's a multidimensional php array)
Now, if in javascript I want to access the value of "United States", I do this:
alert (jsArray[0][0]);
But I get "{" as a result. (a left curly brace). I'm totally stumped, and I know this should be simple.
How can I access parts of my Javascript array?
Maor Barazany answers:
Try to use this:
<?php
$myArray = array (
0 => array (
0 => 'United States',
1 => '700 Trapp Hill Road',
2 => 'Stowe, Vermont',
3 => 'http://neweddingvenues.com/images/image.png',
4 => '350,000',
5 => 'http://neweddingvenues.com/listing/vermont-venue/'
)
);
?>
<script type="text/javascript">
var jsArray = <?php echo json_encode($myArray); ?>;
alert (jsArray[0][0]);
</script>
John Buchmann comments:
That works in your specific example. That's progress! :) But my PHP array is more specifically built like this:
<begin loop>
$array_mapinfo = array($counter=>array(0 =>'United States', 1=>'700 Trapp Hill Rd', 2=>'Stowe, Vermont', 3=>'http://www.sample.com/image.png', 4=>'350,000', 5=>'http://www.sample.com'));
$counter=$counter + 1;
<end loop>
If I hard code $counter to be a number, then it works, but I need it to be a variable that increments in a loop. When I do this it doesn't work.
Maor Barazany comments:
What is the value of your array after the loop?
Paste here please var_dump($array_mapinfo) after the loop.
Paste the loop also.
It might be that you are overriding the array in each iteration.
Try
$array_mapinfo[] =
to add to the array and not overriding it.
Maor Barazany comments:
Try this loop for example:
for ($i=0;$i<4;$i++) {
$array_mapinfo[$i] =
array(
0 =>$i.'-United States',
1=>$i.'-700 Trapp Hill Rd',
2=>$i.'-Stowe, Vermont',
3=>$i.'-http://www.sample.com/image.png',
4=>$i.'-350,000',
5=>$i.'-http://www.sample.com'
)
;
}
?>
<script type="text/javascript">
var jsArray = <?php echo json_encode($array_mapinfo); ?>;
alert(jsArray[2][2]);
</script>
Maor Barazany comments:
Of course, the $i that appears inside the inner array was just for the test of the alert to see it's alerting the right value, you don't really need it in there...
Here's the code without it..
for ($i=0;$i<4;$i++) {
$array_mapinfo[$i] =
array(
0 =>'United States',
1=>'700 Trapp Hill Rd',
2=>'Stowe, Vermont',
3=>'http://www.sample.com/image.png',
4=>'350,000',
5=>'http://www.sample.com'
);
}
John Buchmann comments:
That was it, thanks! I needed to put the $counter part on the main array variable, like this:
$array_mapinfo[$counter] =
array(
0 =>'United States',
1=>'700 Trapp Hill Rd',
2=>'Stowe, Vermont',
3=>'http://www.sample.com/image.png',
4=>'350,000',
5=>'http://www.sample.com'
);
So my PHP array was having the wrong syntax. Thanks so much! :)
Utkarsh Kukreti answers:
var jsArray = <?php echo json_encode(json_encode($myArray)); ?>;
should be
var jsArray = <?php echo (json_encode($myArray)); ?>;
Christianto answers:
Hi,
It works for me with this..
<script type="text/javascript">
<?php
$myArray =
Array (
Array (
0 => "United States",
1 => "700 Trapp Hill Road",
2 => "Stowe, Vermont",
3 => "http://neweddingvenues.com/images/image.png",
4 => "350,000",
5 => "http://neweddingvenues.com/listing/vermont-venue/"
)
);
?>
var jsArray = <?php echo json_encode($myArray); ?>;
alert (jsArray[0][0]);
</script>
Just a syntax problem I guess..
Hope this help..
Romel Apuya answers:
i think this one will work..
<?php
$myArray = Array (
0 => Array (
0 => "United States" ,
1 => "700 Trapp Hill Road" ,
2 => "Stowe, Vermont" ,
3 => "http://neweddingvenues.com/images/image.png",
4 => "350,000",
5=> "http://neweddingvenues.com/listing/vermont-venue/"
),
);
?>
<script type="text/javascript">
var jsArray = <?php echo json_encode($myArray); ?>;
alert (jsArray[0][0]);
</script>
Luis Cordova answers:
use power
use jquery way:
[[LINK href="http://edrackham.com/php/how-to-convert-php-multidimensional-array-to-javascript-object-using-jquery/"]]http://edrackham.com/php/how-to-convert-php-multidimensional-array-to-javascript-object-using-jquery/[[/LINK]]
take advantage of how this should be done