I have a json string sent from html...
[{"user_id":"test_123"},{"id":"wallName","value":"","type":"text"},{"id":"wallLength","value":"","type":"text"}]
I want to retrieve the "user_id":"test_123" and then from that create a folder named test_123, maybe even a matching file named test_123. I'm thinking I need to convert the json file to an array, get the user_id value and convert that back to a string? Does that make sense or am I over complicating this? I'm new to php so that might very well be the case.
Here's my php code...
<?php
$json=$_POST[json];
$decodedText=html_entity_decode($json);
$myArray = json_decode($json, true);
if (json_decode($json) != null){
$file=fopen('user_data.json','w+');
fwrite($file, $json);
fclose($file);
}else{
echo "empty";
}
?>
When I try to access $myArray it doesn't work.
asked Dec 5, 2015 at 3:36
JoMojo
4041 gold badge7 silver badges22 bronze badges
-
json_decode($json) convert your json to object may be...Parth Chavda– Parth Chavda2015年12月05日 03:44:44 +00:00Commented Dec 5, 2015 at 3:44
-
$array = (array) json_decode($json) ; try this may be it's help youParth Chavda– Parth Chavda2015年12月05日 03:45:56 +00:00Commented Dec 5, 2015 at 3:45
-
@ParthChavda Thanks for the suggestions, how woould I access the array if I want 'test_123'? I'm having a hard time figuring that part out. I would think $array[0] would work but it doesn't seem toJoMojo– JoMojo2015年12月05日 03:50:49 +00:00Commented Dec 5, 2015 at 3:50
-
13v4l.org/T7Y9tjayxhj– jayxhj2015年12月05日 03:57:57 +00:00Commented Dec 5, 2015 at 3:57
-
1you can get using $myArray[0]['user_id'];Parth Chavda– Parth Chavda2015年12月05日 03:58:25 +00:00Commented Dec 5, 2015 at 3:58
1 Answer 1
you can get user_id using $myArray[0]['user_id'];
answered Dec 5, 2015 at 4:51
Parth Chavda
1,8291 gold badge24 silver badges31 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-php