0

Im trying to read a value of an ajax send jsonarray to base my filename upon but i cant seem to figure out how to read the value.

my php;

$postdata = $_POST['data'];

$jsondata = json_decode($postdata);
$myname = $jsondata->name;

$dir = 'users/'.$myname.'/desktop-'.$myname.'.json';

json array looks like this;

[{"name":"mmichel"},
 {"myicons":
    [{"icon":
       [{"name":"homepagelink","rel":"http://test.tocadovision.nl","id":"icon1","class":"icon bookmark"}]
     },
     {"icon":
       [{"name":"aboutpagelink","rel":"http://test.tocadovision.nl","id":"icon2","class":"icon bookmark"}]
    }]
}]

hope someone can tell me what im doing wrong.. must be rly easy i guess

2 Answers 2

1

Since $jsondata contains an array of one object, you need to access the first element of the array in your assignment:

$myname = $jsondata[0]->name;
Sign up to request clarification or add additional context in comments.

2 Comments

i thought so too, even before your post, but somehow it isnt :(
@MartijnMichel - then you need to go through your code and debug each variable until you work out why
0

Ensure you have json enabled in your php config. You can do this by doing

<?php
phpinfo();

This should output something like this which indicates json module is enabled. If you don't have this enabled, enable it.

enter image description here

Make sure you have the correct valid json string in $json variable and use one of the following methods.

$arr = json_decode($json, true);
print_r($arr[0]['name']);


$arr = json_decode($json);
print_r($arr[0]->name);

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.