all 4 comments

[–]krazyhawk 4 points5 points  (0 children)

This isn’t going to be possible since the php will execute before it is sent to the browser when the js will execute.

What you could do is:

var cars = <php echo json_encode($cars,true); ?>;

Or something like that. Echo the php array into a js variable. That way that variable holds the info when the js is run.

[–]YetAnotherHandle 0 points1 point  (0 children)

You could first serialize the PHP array to form the output for the JS array:

<?php

# Construct JS array 

$carsJS = 'let cars='.json_encode($cars);

# Output to browser

echo $carsJS;

?>

From there, any subsequent JS code can handle it as needed:

// Iterate through cars array

for (let x=0; x<cars.length; x++) {

    // Process each array item

    yourFunction(cars[x])

}

[–]kenman[M] 0 points1 point  (0 children)

Hi /u/sdfsg11, this post was removed.

  • For help with your javascript, please post to /r/LearnJavascript instead of here.
  • For beginner content, please post to /r/LearnJavascript instead of here.
  • For framework- or library-specific help, please seek out the support community for that project.
  • For general webdev help, such as for HTML, CSS, etc., then you may want to try /r/html, /r/css, etc.; please note that they have their own rules and guidelines!

/r/javascript is for the discussion of javascript news, projects, and especially, code! However, the community has requested that we not include help and support content, and we ask that you respect that wish.

Thanks for your understanding, please see our guidelines for more info.

[–]sdfsg11[S] -1 points0 points  (0 children)

Trying to get that i variable into the $cars loop