Alexander
Alexander

Reputation: 2463

php to js array convert with jquery's ajax

Im trying to convert 5 PHP arrays to 5 js arrays.

I used to transfer php variables to js variables with json like this:

$return['variable'] = $variable;
echo json_encode($return);

And then fetch it as json object on the js side like this:

success : function(data) {

    alert(data.variable);

}

now things are a bit more complicated, i need to transfer these 5 php arrays from a php script to my js script as 5 js arrays:

PHP arrays:

$i = 0;
while ($location = mysql_fetch_array($get_locations)) {

    $location_full_name[$i] = $location['loc_full_name'];
    $location_main_name[$i] = $location['loc_main_name'];
    $location_sub_name[$i] = $location['loc_sub_name'];
    $location_anchor_id[$i] = $location['loc_anchor_id'];
    $location_type[$i] = $location['loc_type'];

    $i++;
}

and fill these corresponding arrays:

var location_full_name = new Array();
var location_main_name = new Array();
var location_sub_name = new Array();
var location_anchor_id = new Array();
var location_type = new Array();

i dont know how to do this. hope i can get some help :)

regards, alexander

Upvotes: 0

Views: 1639

Answers (3)

moey
moey

Reputation: 10887

You can utilize the PHP array that is actually an ordered map. Below is an example:

PHP:

<?php
$location_full_name = array("Google, Inc.", "Siku-Siku.com");
$location_type = array("Google headquarter", "Virtual address");
$ret = array("full_name" => $location_full_name, "type" => $location_type);
echo json_encode($ret);
?>

JavaScript (jQuery):

<script type="text/javascript">
$(function() {
    $.get("test.php", function(data) {
        console.log(data.full_name[1]); // Prints "Siku-Siku.com".
    }, "json");
});
</script>

Upvotes: 0

Joseadrian
Joseadrian

Reputation: 4374

Maybe if you post what returns in "data" so we can help you more (i think). hehe.

I suggest, for your php code, where you set the data into the arrays:

$i = 0;
$rsl = array();
while ($location = mysql_fetch_array($get_locations)) {
    $rsl[$i]['full_name'] = $location['loc_full_name'];
    $rsl[$i]['main_name'] = $location['loc_main_name'];
    $rsl[$i]['sub_name'] = $location['loc_sub_name'];
    $rsl[$i]['anchor_id'] = $location['loc_anchor_id'];
    $rsl[$i]['type'] = $location['loc_type'];
    $i++;
}
echo json_encode($rsl);

So to get this on the javascript

// You could do the same... var location = []...
var location_full_name = new Array();
var location_main_name = new Array();
var location_sub_name = new Array();
var location_anchor_id = new Array();
var location_type = new Array();
...
dataType: "json",
success : function(data) {
     $.each(data, function(index, arr){
          location_full_name[index] = arr['full_name'];
          ...
     });
}

Upvotes: 2

Kevin Peno
Kevin Peno

Reputation: 9196

For each of your arrays, store the value returned from json_encode. And/or make them one array/object and do the same.

Upvotes: 0

Related Questions