1

i have this variable $a

var_dump($a)

 array(3) { [0]=> string(10) "designer" [1]=> string(8) "director" [2]=> string(10) "Freelancer"} 

i am sending this in ajax(jquery)

jquery

  data: 'form=<?php echo json_encode($a); ?>',

and in the other php file i am doing

$send = $_POST[form];

$b = json_encode($send);

$c = json_decode($b, true);
var_dump($c);

the output will be:

string(xx) "[\"designer\",\"director\",\"Freelancer\"]" 

but, echo $c[0] show this: [ and should be "designer"

Any help ?

EDIT: already tried too

$send = $_POST['form'];

$c = json_decode($send, true);

var_dump($c);

output: `null`
5
  • pls give us var_dump($send) result. I'm not sure 'bout what output do u mean. Commented Oct 31, 2011 at 3:31
  • updated, sorry. Var_dump $c, is correct now Commented Oct 31, 2011 at 3:35
  • $_POST[form] is probably incorrect, unless you are using a constant named form. Commented Oct 31, 2011 at 3:40
  • I don't think you need $b = json_encode($send); Commented Oct 31, 2011 at 3:42
  • the problem is caused by initial array. with this <?php $a = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5); ?> there is no problem. Commented Oct 31, 2011 at 4:32

2 Answers 2

2

Because $c is actually the string "[\"designer\",\"director\",\"Freelancer\"]", and not the array ["designer", "director", "Freelancer"]. It looks like you're calling json_encode on your content twice, and json_decode once.

  1. form=<?php echo json_encode($a); ?> will encode your content once, before sending it over the wire.
  2. $send = $_POST[form]; will get that content (already json_encoded).
  3. $b = json_encode($send); will encode that same content a second time.
  4. $c = json_decode($b, true); will decode it.

This will leave you with your content still encoded. I'm not quite sure what the point of step 3 is, and it looks to me that removing it should solve your problem.

EDIT:

Since you've updated the question stating that you get null if you try the proposed solution, according to the PHP documentation for json_decode:

NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

You should make sure that the data is set to exactly what you want, as I don't think your recursion level is too deep in this case (from the data you've given, it appears as if there is none whatsoever).

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

7 Comments

I've updated my answer with a bit more information. Have you tried seeing what the value of $send is?
var_dump[$send] is equal to var_dump($c), with or without json_encode repeated –
So without $b = json_encode($send), $send is null? But with it, $send is "[\"designer\",\"director\",\"Freelancer\"]"? That makes no sense... are you certain nothing else is changing?
is strange, but it's happening what you say.
...not that this is any kind of usable solution, but... what happens if you encode twice and decode twice?
|
0

$_POST[form] already is a JSON encoded string. If you encode it again, then decode it, you'll end up with the same string, not an array. You don't need $b = json_encode($send).

It also looks like you have Magic Quotes on. Turn them off or follow the documentation to clean your strings: http://www.php.net/magic_quotes

Also, quote your array indices!

$send = $_POST['form'];

http://www.php.net/manual/en/language.types.array.php#language.types.array.donts

1 Comment

i already try with different ways. With the change that you suggest i simple get null.

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.