0

i have

 $age = implode(',', $wage);   // which is object return:  [1,4],[7,11],[15,11]
  $ww = json_encode($age);

and then i retrieve it here

    var age = JSON.parse(<?php echo json_encode($ww); ?>); 

so if i make

   alert(typeof(<?php echo $age; ?>))   // object
   alert(typeof(age))                   //string

in my case JSON.parse retuned as string.

how can i let json return as object?

EDIT:

 var age = JSON.parse(<?php echo $ww; ?>); // didnt work , its something syntax error
echo_Me
  • 37,078
  • 5
  • 58
  • 78
  • `$ww=json_encode($age)` then you echo `json_encode($ww);` so really `var age=JSON.parse(` Is this what you really want to be doing? – Popnoodles Dec 08 '12 at 21:58
  • why are you using json_encode twice? In the second time you are basically running json_encode on a string, which doesn't make sense, since json_encode runs on arrays\objects and turns them into strings – Matanya Dec 08 '12 at 22:02
  • i edited my post as u told me @Matanya – echo_Me Dec 08 '12 at 22:09

3 Answers3

2

implode returns a string, so it is only natural that json_encode encodes it as such. It does not recognize already JSON-like data passed as a string.

If you want to get an object, you have to pass an associative array to json_encode:

$foo = array(
    1 => 4,
    7 => 11,
    15 => 11
);

echo json_encode($foo); // {1:4,7:11,15:11}

With so little info about what $wage looks like before it's imploded, it's hard to tell exactly what you want to get. How is that structure ([1,4],[7,11],[15,11]) an object? Is the first element of each tuple a key? That's what I assumed with my example, but it might be off.

zneak
  • 134,922
  • 42
  • 253
  • 328
  • but why type of $age is object and type of age is string in my code – echo_Me Dec 08 '12 at 22:01
  • @peter, you have to look at the code this generates. You first example does `typeof([1,4],[7,11],[15,11])`, and `typeof` on an array returns `object` (put simply, `typeof` returns the type of the last comma-separated value it receives as an argument). In your second case, you get `typeof("[1,4],[7,11],[15,11]")`, which definitely is a `string` literal (notice the quotes). – zneak Dec 08 '12 at 22:04
  • i didnt make `typeof("[1,4],[7,11],[15,11]")` with double quotes – echo_Me Dec 08 '12 at 22:07
  • @peter, you did not exactly do it, but you did `var age = JSON.parse("\"[1,4],[7,11],[15,11]\"");` and then `typeof(age)`. (Look at how you call `json_encode` twice, once on `$wage` to get `$age` and then again on `$age` to put it in your Javascript.) – zneak Dec 08 '12 at 22:08
  • so how to fix this then? to remove those double quotes .thx for the help – echo_Me Dec 08 '12 at 22:11
  • Don't call `json_encode` twice. Just `echo $ww` instead of `echo json_encode($ww)`. Though this will still probably not give you what you expect as it will return `[15,11]` only. This is the part where you need to clarify what you want, and in which for you want it. Why don't you edit your question to include exactly the JSON string you would like to get? – zneak Dec 08 '12 at 22:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20819/discussion-between-zneak-and-peter) – zneak Dec 08 '12 at 22:14
1

a. You get a syntax error because you need to enclose the string within quotes, like so:

var age = JSON.parse("<?php echo $ww; ?>");

b. Moreover, you don't need JSON.parse. You can simply echo the php var after it was already json_encoded in the server side:

var age = <?php echo $ww; ?>;

JSON.parse is there to convert a JavaScript string to an object. In the case of PHP string, once it is built as JSON, echoing it in the right place is equivalent to coding it yourself.

Matanya
  • 6,233
  • 9
  • 47
  • 80
1
var age = [<?php echo $age; ?>];
James
  • 20,957
  • 5
  • 26
  • 41