I have a one (very) big PHP array (with 649 indexes). I want to set a javascript variable to specific index of it and that index will depend on another variable. Is there any way? I know i can copy whole PHP array into JavaScript Array using json_encode($phpArray)
, but i don't want whole array to be loaded on client side (for speed and some sort of security).
$phpArray[0] = "i am first element";
$phpArray[1] = "i am second element";
/* ......... */
$phpArray[100] = "i am 100th element";
and now, let us say, i want to get second element of $phpArray
? how can i do it?
Here is an example using jQuery $.post
method:
$(document).ready(function(){
var my_var;
$.post("my-php-page.php",
{my_index: 1},
function(data) {
my_var = data.my_value;
},
"json");
});
my-php-page.php
<?php
$phpArray[0] = "i am first element";
$phpArray[1] = "i am second element";
/* ......... */
$phpArray[100] = "i am 100th element";
echo json_encode(array("my_value" => $phpArray[$_POST['my_index']]));
?>
Is this what you want?