php - Json array encode and decode the same -
i encode array json format using code below :
$arrayname[] = array('user_role_id' => 1, 'user_id' => 1); $arrayname[] = array('user_role_id' => 2, 'user_id' => 2); $jos = json_encode(array_values($arrayname));
the output is
[{"user_role_id":1,"user_id":1},{"user_role_id":2,"user_id":2}]
and sent browser.
then after steps browser returns same array.
but when try decode
$return = json_decode($jos,true);
i receive error : array string conversion.
browser didnt array. returns supply.
code flow :
<?php if(isset($_get['array'])) { $arrayname[] = array('user_role_id' => 1, 'user_id' => 1); $arrayname[] = array('user_role_id' => 2, 'user_id' => 2); header('content-type: application/json'); echo $jos = json_encode($arrayname); } else { if(isset($_post['jos'])) { $jos = $_post['jos']; echo $new_jos = json_decode($jos,true); } }
can please me?
what might wrong?
the problem line:
echo $new_jos = json_decode($jos,true);
so convert json string array(that works!) , echo - throws error.
just use
$new_jos = json_decode($jos,true);
Comments
Post a Comment