JoMojo
JoMojo

Reputation: 404

php convert json string to array

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.

Upvotes: 2

Views: 104

Answers (1)

Parth Chavda
Parth Chavda

Reputation: 1829

you can get user_id using $myArray[0]['user_id'];

Upvotes: 2

Related Questions