0

I explored a bunch of related questions but to no avail. I use json_encode to pass on a PHP variable to JS and it works fine. <?php echo json_encode($datasets[0]);?>);

But when I try to do the same thing inside a JS for loop using the index of the loop it only picks up null. How can I use the the index of the PHP array $datasets[] inside a JS for loop?

const DT = [];
    for (let i = 0; i < 10; i++) {
    DT.push(<?php echo json_encode($datasets[i]);?>);
    };
Naim
  • 471
  • 1
  • 4
  • 14

2 Answers2

2

PHP executes before any of the JavaScript executes, so it cannot evaluate $datasets[i], because that i is not known in PHP. Moreover, it would only evaluate it once, because there is no PHP loop.

The way to do this, is to let PHP pass the whole data set to JavaScript:

const DT = <?php echo json_encode(array_slice($datasets, 0, 10));?>;

If the PHP array is also 10 items long, then you can omit the array_slice part. But if your intention was to just get the first 10 items of a longer PHP array, you'll need it.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • I need my ear to be pulled hard! Again forgot the array mechanism is built to handle what I needed, no need for a for loop to synchronize indexes between 2 languages that do not speak to each other out of the box. Thanks so much. Performance took a hit though, any way to lower response time? – Naim May 07 '22 at 16:01
  • How large is the array? – trincot May 07 '22 at 16:05
  • It's only 10 in length as I am trying to display a top 10 on a chart.js chart. I would probably be better off parsing each element separately but just out of curiosity wondering if there is a faster way for large datasets. Thanks again. – Naim May 07 '22 at 16:10
  • *"better off parsing each element separately"*: definitely not. A "top 10" sounds like the PHP array is longer than 10, and then you really need to include that `array_slice` part. – trincot May 07 '22 at 16:15
  • 1
    It seems performance hit came from the host, just a pure coincidence. Array is 10 only and its working flawlessly. Thanks. – Naim May 07 '22 at 17:05
1

you are missing some quotes and you can't iterate over a php object in javascript

it should be

const DT = JSON.parse('<?php echo json_encode($datasets);?>')
    

or if you want to do a for loop for some reason you could do this

const DT = [];
<?php for($i = 0; $i < 10; $i++): ?>
DT.push(JSON.parse('<?php echo json_encode($datasets[$i]);?>'))
<?php endfor; ?>

R4ncid
  • 6,944
  • 1
  • 4
  • 18
  • Thanks so much and your answer worked too although I did not have to use a for loop in the first place but assuming I had to, your answer would be the best one. It also showed me how to push PHP into a JS array and there was no chance for me to see the need for `JSON.parse` as I thought `json_encode` would be sufficient. Thanks again. – Naim May 07 '22 at 19:38
  • oh my! I just noticed how you initiated the loop in PHP so that you could pick up the index later on when iterating through the PHP variable...very very clever! thanks a bunch for the eye opener. Also, now I see the why `JSON.parse` was needed. – Naim May 07 '22 at 19:45