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
3
  • $ww=json_encode($age) then you echo json_encode($ww); so really var age=JSON.parse(<?php echo json_encode(json_encode($age)); ?> Is this what you really want to be doing? Commented Dec 8, 2012 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 Commented Dec 8, 2012 at 22:02
  • i edited my post as u told me @Matanya Commented Dec 8, 2012 at 22:09

3 Answers 3

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.

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

7 Comments

but why type of $age is object and type of age is string in my code
@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).
i didnt make typeof("[1,4],[7,11],[15,11]") with double quotes
@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.)
so how to fix this then? to remove those double quotes .thx for the help
|
1
var age = [<?php echo $age; ?>];

Comments

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.

7 Comments

first i dont see where i have writed double quotes as u mentioned
a. You didn't. You should have. b. As I said, this is redundant
b variable age is also string without json parse
removed json parse but it same , its string for variable age
go to view source in the browser please and paste it here so I can see how it is parsed
|

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.