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`
Because
$cis actually the string “[\”designer\”,\”director\”,\”Freelancer\”]”, and not the array["designer", "director", "Freelancer"]. It looks like you’re callingjson_encodeon your content twice, andjson_decodeonce.form=<?php echo json_encode($a); ?>will encode your content once,before sending it over the wire.
$send = $_POST[form];will get that content (alreadyjson_encoded).
$b = json_encode($send);will encode that same content a secondtime.
$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
nullif you try the proposed solution, according to the PHP documentation for json_decode: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).