This question already has answers here:
How to get element from json with php? [duplicate]
(2 answers)
Closed 3 years ago.
I have a string variable in this format:
$array = '[{"name":"jack","address":"who knows"},{"name":"jill","address":"who knows too"}]';
how can I get value from
$array[0]['name']; //value is 'jack'
https://juderosario.com/2016/09/01/parsing-json-with-php/
ok I get it from there
$json = '[ {"id":"1", "name":"foo" }, {"id":"2", "name":"bar" } ]';
$baz = json_decode($json,true);
echo($baz[1]['name']); //bar
Related
This question already has answers here:
Php how to parse Json
(4 answers)
Closed 4 months ago.
I have this array output :
{"NoID":5645656956,"dispositionMessage":"ISSUED","binarySecurityToken":"TUlJQ1VEQ0NBZldnQXdJQkFnSUdBWVBDdjV4OE1Bb0dDQ3FHU000OUJBTUNNQlV4RXpBUkJnTlZCQU1NQ21WSmJuWnZhV05wYm1jd==","number":"1234568","errors":null}
and want to save these array outputs inside these variables:
$NoID = 5645656956;
$binarySecurityToken = TUlJQ1VEQ0NBZldnQXdJQkFnSUdBWVBDdjV4OE1Bb0dDQ3FHU000OUJBTUNNQlV4RXpBUkJnTlZCQU1NQ21WSmJuWnZhV05wYm1jd==;
$number = 1234568;
Thanks for all
You need to decode the string into an array or object. Then you can access all the properties.
$jsonString = '{"NoID":5645656956,"dispositionMessage":"ISSUED","binarySecurityToken":"TUlJQ1VEQ0NBZldnQXdJQkFnSUdBWVBDdjV4OE1Bb0dDQ3FHU000OUJBTUNNQlV4RXpBUkJnTlZCQU1NQ21WSmJuWnZhV05wYm1jd==","number":"1234568","errors":null}';
$json = json_decode($jsonString);
$noId = $json->NoID;
$binarySecurityToken = $json->binarySecurityToken;
$number = $json->number;
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I have this string array:
["652","110","111","1032","118","416","147","121","112","1033","113","1031","868"]
I need to read each value so to get
652
110
111
1032
i try to convert string array using explode and then foreach but is not working...
$channels = explode('"', $string_array);
foreach($channels as &$channel) {
echo $channel.'<br>';
}
it's an JSON format, so use json_decode
$json = '["652","110","111","1032","118","416","147","121","112","1033","113","1031","868"]';
$array = json_decode($json, true);
foreach($array AS $channel) {
echo $channel.'<br>';
}
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Parse JSON string contents into PHP Array
(4 answers)
Closed 5 years ago.
I have strings like this:
$T = '[{"X":"13","Y":"76", "R":"90"},{"X":"12","Y":"24","R":"-19"}]}';
$A = '[{"X":"1","Y":"6", "R":"0"},{"X":"1","Y":"4","R":"9"},
{"X":"12","Y":"24","R":"-19"}]}';
I want to find each X value in the string, and get the value after them.
Note that the number of X values differs.
For example, I want:
13, 12, 1, 1, 12
I tried explode(), but that only grabbed the first X value (13). I am new to PHP, and don't know an elegant solution. Any suggestions are appreciated.
Remove the last curly brace (}) of your strings to have valid json, i.e:
$A = '[{"X":"1","Y":"6", "R":"0"},{"X":"1","Y":"4","R":"9"},
{"X":"12","Y":"24","R":"-19"}]}';
$A = trim($A, '}');
$json = json_decode($A, true);
foreach($json as $value)
{
echo $value['X'];
}
# 1112
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 7 years ago.
I Want to convert the string into an array and return the rating value. I've been able to return it as a string so its nothing wrong with the first line of code.
JSON:
{
"id": "23",
"rating": "31"
}
Code:
$json = file_get_contents('http:*****='.$film->id.'') ;
$array = json_decode($json,TRUE);
echo $array[1] ;
Line 3 should be:
echo $array['rating'];
This question already has answers here:
PHP string to array
(4 answers)
Closed 8 years ago.
I have a complete string how can i get some part of it and insert into an array this is my string in php
[{"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"},{"albumid":"EmgsZ43ehT","albumname":"testalbum","type":"1","access":"1","itemcount":"0"},{"albumid":"Jf4H4SvFGk","albumname":"test2album","type":"3","access":"1","itemcount":"0"},{"albumid":"k3pacBSmIl","albumname":"testalbumpvt","type":"3","access":"2","itemcount":"0"}]
i want something similar to this
$value1 = $array[0];
// {"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"}
is this possible to get each value like this
$value1 = $array[0]['albumid'];
// ASaBFzCtl8
Yes use json_decode()
$j = '[{"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"},{"albumid":"EmgsZ43ehT","albumname":"testalbum","type":"1","access":"1","itemcount":"0"},{"albumid":"Jf4H4SvFGk","albumname":"test2album","type":"3","access":"1","itemcount":"0"},{"albumid":"k3pacBSmIl","albumname":"testalbumpvt","type":"3","access":"2","itemcount":"0"}]';
$data = json_decode($j,true);
You can use loop to read the data as
foreach($data as $key=>$val){
echo $val["albumid"]."<br />";
}
Above will just get the albumid you can read whatever you want from this array.