WChris
WChris

Reputation: 71

PHP Array to JS array and restructure

I have two PHP strings that looks like this (obviously shortened for the question):

 $year [
    [0]=>2003
    [1]=>2003

    [2]=>2004
    [3]=>2004

    [4]=>2005
    [5]=>2005



]

 $cost [
    [0]=>200
    [1]=>300
    [2]=>400
    [3]=>500
    [4]=>410
    [5]=>510
]       

I need to turn the PHP arrays above into a JAVASCRIPT array formatted exactly as below:

     var newData=[            
          ['2003', 200,300],
          ['2004', 400,500],
          ['2005', 410,510]       
        ];

When I type in the JS array implicitly (as above) all works fine (ie the graphics render correctly). However after several hours of trying different approaches, I cannot work out how to easily generate the newData array dynamically (ie from PHP arrays as shown) in exactly the JS Array format shown.

Upvotes: 0

Views: 72

Answers (3)

WChris
WChris

Reputation: 71

Thanks for all the help. Your answers showed I was going about things the right way (json_encode etc). What has happened though is that the legacy system producing the PHP Arrays was not correcting the values for integers rather than strings. The recipient plug in needed INTs -- see the output array format in the question

So json_encoding the php array worked OK - but it was encoding string rather than INT data values. Fixed that and now it all seems fine.

PS If you look at the orginal question yyou will see the OP array in JS needed two datatypes. And of course it was only getting strings. Lesson there for sure!

Heres the snippet

             var a = ['1','2','3'];
             var result = a.map(function (x) { 
             return parseInt(x, 10);

Upvotes: 1

BigBlast
BigBlast

Reputation: 387

if you respect the structure in the example, the following would do the job:

<?php

$year = [
   0 => 2003,
   1 => 2003,
   ...
];

$cost = [
   0 => 200,
   1 => 300,
   ...
];

for($i=0;$i<SIZE_OF_ARRAY;$i+=2)
     $newData[] = [(string) $year[$i], $cost[$i], $cost[$i+1]];

?>

Now in the javascript portion of the code you just need:

<script>
    var newData = <?= json_encode($newData); ?>
</script>

Note that i didnt use the quotes between the php code because i do want the javascript to parse the php output as javascript code and not as a javascript string.

Upvotes: 1

Philipp Maurer
Philipp Maurer

Reputation: 2505

Your quest can be separated in two tasks: First of all you want to combine both arrays. I guess you want to do this based on the keys they have. There are multiple functions for that in PHP, like array_combine or array_reduce. Yet for your case the easiest way is a for_each loop, because you have duplicate values in the $year array.

$combined_array = [];
foreach ($year as $id => $year_val) {
    if (!array_key_exists($year_val, $combined_array)) {
        $combined_array[$year_val] = [(string)$year_val];
    }
    $combined_array[$year_val][] = $cost[$id];
}

Now you have the years as keys, what you do not want, so you can use array_values to remove the keys again.

$combined_array = array_values($combined_array);

The second task is quite easy: Go to the PHP file, that loads the script you want to provide the array to. Add this before you load the script:

<script>
    var myPhpArray = <?php echo json_encode($combined_array) ?>;
</script>

After that the PHP array is accessible from JS in the variable `myPhpArray.

Upvotes: 1

Related Questions