0

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

3 Answers 3

2

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'];
          ...
     });
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks :D that was very neat! just a little question, how can i do a callback after the each loop is finished? how would the syntax look like?
I which i could help you but i don't know what a callback means :(
raped and pillaged? See, basically, the exact same anwwer that was posted before you from @offex
0

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

2 Comments

hi there, i didnt really understand that fully. could you come with an example? :)
Hey, check the example in the next answer. I was beaten to providing it.
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.