GrumpyCrouton
GrumpyCrouton

Reputation: 8621

Passing nested PHP loop to javascript

I have a PHP array that looks like this:

([NAME] => Array ( [0] => 16 [1] => 12 [2] => 4 [3] => 0 ))
([NAME2] => Array ( [0] => 19 [1] => 19 [2] => 0 [3] => 0 ))
([NAME3] => Array ( [0] => 31 [1] => 29 [2] => 2 [3] => 0 ))

This array is generated by PHP by pulling data from a database. (The array is much larger than this, but for simplicity, I only included 3 records)

I really need this data to be usable in a script that generates a bar graph. For now, I only need the name and the first record. So, for example, in PHP

<?=$array['NAME'][0]?>
<?=$array['NAME2'][0]?>

would echo the information I need from each, but I'm not sure how to put that into the format I need into javascript.

This is the javascript format that I need:

        { tech: 'NAME', value: $array['NAME'][0] },
        { tech: 'NAME2', value: $array['NAME2'][0] },
        { tech: 'NAME3', value: $array['NAME3'][0] },
        { tech: 'NAME4', value: $array['NAME4'][0] },
        ...etc

Some type of loop would be preferred because the names can vary. If it was just PHP I could do it, but I'm not too fluent in Javascript. Could someone help me come up with a solution?

Upvotes: 1

Views: 299

Answers (5)

dmgig
dmgig

Reputation: 4568

The good news is that you don't have to know much javascript. Create your data structure as you want it in php, and then use json_encode: http://php.net/manual/en/function.json-encode.php

<?php
        $for_chart = array();
        foreach($names as $key => $name){
           $obj = new stdClass;
           $obj->tech = $key;
           $obj->value = $name[0];
           $for_chart[] = $obj;
        }
    ?>   

the result is:

[{"tech":"NAME","value":1},{"tech":"NAME2","value":5},{"tech":"NAME4","value":9}] 

You can use it in your javascript like this:

<script>
    var my_names = <?=json_encode($for_chart)?>;
    alert(my_names[0].value); // alerts "1"
</script>

http://sandbox.onlinephpfunctions.com/code/2682f09fd5515b220402db9c600b70a0501a87d9

Upvotes: 2

Anthony
Anthony

Reputation: 71

If you json_encode the array you have, it comes out to:

{"NAME1":[16,12,4,0],"NAME2":[19,19,0,0],"NAME3":[31,29,2,0]}

which will give you a javascript object to iterate:

If you have the json object in a variable called arrayData:

var gridData = [];
for (var name in arrayData) {
  var rowObj = { tech: name, value: arrayData[name][0] };
  gridData.append(rowObj);
}

At the end of that, you will have an array of objects that should be what you're looking for in gridData.

Upvotes: 1

ameenulla0007
ameenulla0007

Reputation: 2683

//Assuming this is your array
$getArray["NAME"]   = array(16,12,4,0);
$getArray["NAME2"]  = array(19,19,0,0);
$getArray["NAME3"]  = array(31,29,2,0);
$newArray   = array();
foreach($getArray as $indKey=>$indArrVal) {
    $newArray[] = array("tech"=>$indKey, "value"=>$indArrVal[0]);
}
echo json_encode($newArray);

here it is, create a new array, with the required indexes and then stringify it using json_encode

Upvotes: 0

kpbl
kpbl

Reputation: 24

To get the javascript code just iterate over the php array an echo the json records like you want:

<?= foreach($array as $key => $value){
     echo "{ tech: '" .$key ."', value: '" .$value[0] ."'},";
   }
?>

Upvotes: 0

Graham
Graham

Reputation: 1960

If you would echo it in the following format you can JSON to get the data:

[{tech: 'NAME', value: $array['NAME'][0]},{tech: 'NAME', value: $array['NAME'][0]}]

In your js you could do something like the following:

var names = $.getJSON('http://localhost/index.php?name', function(data) {});

This way you have an array in js.

Upvotes: 0

Related Questions