I got from web api [{"id":45,"username":"xx@xxxx.xx"}]
the list of objects, actually I know for sure, that always there will be only one item in the list (like in example above), how could I translate this string to the array, to get the information by arr['id']
Asked
Active
Viewed 137 times
0

Sergey Vladimirovich
- 479
- 1
- 8
- 15
-
Use [`json_decode()`](http://php.net/manual/en/function.json-decode.php) function. – Rajdeep Paul Jul 22 '17 at 10:11
1 Answers
2
$var = json_decode('[{"id":45,"username":"xx@xxxx.xx"}]') ;
print_r($var) ;

Pierre Granger
- 1,993
- 2
- 15
- 21
-
I tried it, after I use file_put_contents('check.txt', PHP_EOL . print_r($var), FILE_APPEND); to writes the results in file, and it shows "1".... don't you know why? – Sergey Vladimirovich Jul 22 '17 at 10:20
-
1print_r make a print and return 1 when finished. If you want print_r to return the value you need print_r($var,true) instead of print_r($var) ; – Pierre Granger Jul 22 '17 at 10:29
-
1@SergeyVladimirovich Try this : ` $val){ echo $key. ' = ' . $val.'
'; } ?>` – Ankit Singh Jul 22 '17 at 10:41