2

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.

8
  • json_decode($json) convert your json to object may be... Commented Dec 5, 2015 at 3:44
  • $array = (array) json_decode($json) ; try this may be it's help you Commented 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 to Commented Dec 5, 2015 at 3:50
  • 1
    3v4l.org/T7Y9t Commented Dec 5, 2015 at 3:57
  • 1
    you can get using $myArray[0]['user_id']; Commented Dec 5, 2015 at 3:58

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.