0

I have an array in php like this:

$array_php = (1,2,3,4,5);

In JavaScript I do:

var obj = <?php echo json_encode($array_php); ?>;

If I do alert(obj); I get the content well without problems: 1,2,3,4,5

If I do:

var elem = obj.split(',');

This fail. If I do alert(elem[1]) for example I don't get anything. And the line var elem = ... fails.

If I create the array without json_enconde works fine, but I need access to this object.

What can I do? Thanks!

8
  • 1
    You probably need to do something like: var obj = JSON.parse(<?php echo json_encode($array_php); ?>);. Commented Apr 4, 2014 at 10:22
  • Yeah. I agree. So, then you can iterate over obj using arr = $.pareseJSON(obj), accessing each object in a for loop! Commented Apr 4, 2014 at 10:25
  • 1
    @Andy: Nope. JSON is a subset of JavaScript literal notation, so if you're dropping it into JavaScript source, the JavaScript parser can handle it directly. (In fact, if you did what's in your comment, you'd get a parsing error, because it would become JSON.parse([1,2,3,4,5]) which triggers toString on the array giving us JSON.parse("1,2,3,4,5"), which fails because "1,2,3,4,5" is invalid JSON.) Commented Apr 4, 2014 at 10:25
  • @Andy:No you don't need to parse again on JS. It will get already JSON object at JS when you use json_encode() Commented Apr 4, 2014 at 10:26
  • I learn something new every day. Commented Apr 4, 2014 at 10:27

2 Answers 2

3

alert(obj) converts the array to a string. Array-to-string conversion in JavaScript is essentially done with this.join(",") (not exactly, but close enough).

You don't have to do anything to obj to make it an array, it is an array! So just access alert(obj[1]) and you'll get 2.

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

Comments

1

That's not a PHP array. Do you mean array(1, 2, 3, 4, 5);?

Using json_encode like this creates a string with an JavaScript object or array literal, but since you are outputsing it directly into JavaScript, it's not a string, but allready an array in JavaScript. Look at the generated source code. It will be:

var obj = [1, 2, 3, 4, 5];

not

var obj = "[1, 2, 3, 4, 5]";

and also not

var obj = "1, 2, 3, 4, 5";

So you don't need to split it (there is no string to split). Just access the object (or in this case array) directly:

alert(obj[0]); // Shows 1

Hint: Don't use alert() for debuggging. Use console.log() instead and look at the console. There it's easier to see that it's an array and not a string.

1 Comment

"Hint: Don't use alert() for debuggging. Use console.log() instead... Hint: Don't use console.log for debugging. Use a debugger for debugging. There's one built into your browser, which lets you single-step through the code, inspect variables, etc. :-) +1 for excellent info (including that hint).

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.