0

I'm having a bit of trouble with an annoying ',' during the iteration of a PHP array to produce a Javascript array. Essentially, what I have is this:

<?php
  $array = array(StdObject,StdObject,StdObject);
?>

//later that page...in JavaScript

var list = [
<?php foreach($array as $value):?>
  '<?=$value->some_letter_field?>',
<?endforeach;?>
];

Unfortunatly, what this code does is produce output that looks like this:

var list = ['a','b','c',];

Notice that extra comma in the JavaScript array? This is causing some issues. How would I go about re-writing this PHP snippet so that extra comma doesn't get printed, producing a properly formatted JavaScript array?

The expected output should be:

var list = ['a','b','c'];

I appreciate your help in advance.

6 Answers 6

9

You don't need to do this yourself, PHP has a function called json_encode that does what you want. If for some reason you don't have PHP 5.2.0, there are a lot of implementations in the comments of that page to get around that.

Sign up to request clarification or add additional context in comments.

1 Comment

My apologies. I should have made the question clearer. The PHP array actually contains a list of stdObjects. Using json_encode() doesn't quite work.
5

Use implode() to glue up array elements. It will take care about commas

//later that page..in JavaScript

var list = ['<?=implode("', '", $array)?>'];

1 Comment

I like this solution very much, but it require that the array is flattened before.
1

You can use this to generate the json array:

$list = json_encode($array);

And then read it in Javascript:

var list = <?=$list?>

1 Comment

no need for the php tag its a string that is output as javascript to the browser.
1

This will do the trick:

<?php 
$filtered = array();
foreach($array as $value) {
    $filtered[] = $value->some_letter_field;
}
echo 'var list = ' . json_encode($filtered);
?>

1 Comment

This worked for me, but it is missing a ; in echo 'var list = '.json_encode($filtered).';';
0

How about converting array to a valid JSON object?

var list = JSON.parse("<?php echo json_encode($array); ?>");

Anyway, do you really need to generate JS code on the fly? Isn't there another way to complete your task? JS generation is often considered a hack, and it can be avoided easily in many cases.

4 Comments

The array is used to draw charts in JS where the values are being pulled from the DB.
why would you JSON.parse this when its going to be run in the browser anyway?
@Sinmok if these values are pulled from a DB just make sure you are running proper sanitization on the values
@Sinmok true, there's no real need to parse it, if the JSON objects contains only simple values. However, quite often you want to do additional formatting for complex types, like convert date strings to Date objects. JSON.parse() allows you to specify that formatting callback. But if it's just a bunch of strings or numbers, you are right, .parse() can be omitted
-1

If you insist in keeping your current code for whatever reason, just:

var list = [
<?php foreach($array as $value): ?>
  $output[] = "'".$value->some_letter_field."'";
<?php endforeach; ?>
  echo implode(',', $output);
];

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.