0

I need one help. I am unable to convert string to json array using PHP. I am explaining my code below.

$education=$_POST['education'];

the above line give this output [{'name':'aaa','id':'12'},{'name':'bbb','id':'20'}]. But its coming as a string .I tried to convert into array like below but it gived output as null

$edu=json_decode($education,true);
print_r($edu);

It gives the empty output. Here I need to convert it to array and populate all data. Please help me.

asked May 9, 2017 at 5:54
3
  • 2
    Use double quote for json and try [{"name":"aaa","id":"12"},{"name":"bbb","id":"20"}] Commented May 9, 2017 at 5:59
  • 3
    The JSON string has the wrong quotes. See here, only double quotes are allowed. Commented May 9, 2017 at 6:00
  • Not a valid JSON click here @user6838959 JSON is passed valid test here Commented May 9, 2017 at 6:22

2 Answers 2

1

Hi You need to make your json string like below:

$arr = '[{"name":"aaa","id":"12"},{"name":"bbb","id":"20"}]';
$a = json_decode($arr);
echo "<pre>";
print_r($a);
die;

it will show your output like below:

Array
(
[0] => stdClass Object
 (
 [name] => aaa
 [id] => 12
 )
[1] => stdClass Object
 (
 [name] => bbb
 [id] => 20
 )
)
answered May 9, 2017 at 6:05
Sign up to request clarification or add additional context in comments.

Comments

0

In php, you can use this function to check valid json:

function _isJsonString($string) {
 json_decode($string);
 return (json_last_error() == JSON_ERROR_NONE);
 }

You can also check online for valid json: http://json.parser.online.fr/ Hope this will help.

answered May 9, 2017 at 6:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.