This question already has answers here:
Parsing JSON object in PHP using json_decode
(7 answers)
Closed 4 years ago.
I have json like below, how can i loop each id
{
"id1": {
"AreaN": "\u30ef\u30fc\u30eb\u30c9\u30d0\u30b6\u30fc\u30eb",
"AreaM": "\uff9c\uff70\uff99\uff84\uff9e\uff8a\uff9e\uff7b\uff9e\uff70\uff99"
},
"id2": {
"AreaN": "\u30ef\u30fc\u30eb\u30c9\u30d0\u30b6\u30fc\u30eb",
"AreaM": "\uff9c\uff70\uff99\uff84\uff9e\uff8a\uff9e\uff7b\uff9e\uff70\uff99"
}
}
I tried but it does not get all the ids, i cant change the json.
$json = json_decode($doc);
if( count($json) > 0 )
{
foreach($json as $area)
{
}
}
In my case the ids i want to get are not inside an array. Is it possible to loop the ids and keep them as objects, i prefer object access than using square brackets.
$json = json_decode($doc, 1);
foreach ($json as $id => $area) {
// $id is "id1", "id2"
// $area is AreaN & AreaM
}
the 2nd param for json_decode if true will return as array, otherwise will be object
Related
This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
How can I access an array/object?
(6 answers)
Closed 3 months ago.
I have generated html table data to Json array using jquery, it worked for me and I got the array below, I need php code to iterate each value of this array in php codeigniter 4 controller.
array(1){
[
0
]=>string(966)"[{"ID":"\n2","Name":"\nCP","Price":"\n350.20"},{"ID":"\n3","Name":"\nLFT","Price":"\n700.10"},{"ID":"\n4","Name":"\nRFT","Price":"\n200"},{"ID":"\n5","Name":"\nurinetest","Price":"\n1000"}]"
}
It's a php array that contains a json array, if you are sure you will only have one element in the array, you can do so:
$array = json_decode($var[0]); // $var holds the data you mentioned above
foreach ($array as $item) {
//
}
otherwise:
foreach ($var as $array) { // $var holds the data you mentioned above
$array = json_decode($array);
foreach ($array as $item) {
//
}
}
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I have this JSON
{
"AED_USD": {
"2019-08-01": 0.272242
}
}
When dumping this as an array I have:
array(1) { ["AED_USD"]=> array(1) { ["2019-08-01"]=> float(0.272242) } }
I am trying to reach get the float value of 0.272242.
How do I get this value in PHP?
You have to json_decode the string with true as second parameter to make it an array, and then just echo what you want using the name of the keys you have. Like so:
$string = '{
"AED_USD": {
"2019-08-01": 0.272242
}
}';
$decode = json_decode($string, true);
echo $decode['AED_USD']['2019-08-01'];
To get the value in PHP, you can do
$value = $yourArray['AED_USD']['2019-08-01'];
If you want more information: https://www.php.net/manual/pt_BR/language.types.array.php
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I am trying to use PHP to search the following JSON using CarReg to return the CarID
[
{
"CarID": "f11gh126-dee8-46ef-9665-69119c354575",
"CarReg": "ABCD"
},
{
"CarID": "e258f6d4-4503-5d7e-b25c-1fb9767061e2",
"CarReg": "DEFG"
}
]
How would I retrieve the CarID by searching for the CarReg
First you should convert JSON to array using json_decode() and then use array_filter() to filtering items of array.
$arr = json_decode($json, true);
$carId = array_filter($arr, function($item){
return $item['CarReg'] == 'ABCD';
})[0]['CarID'];
// f11gh126-dee8-46ef-9665-69119c354575
Check result in demo
This question already has answers here:
How to check if variable is array?... or something array-like
(7 answers)
Closed 6 years ago.
When I have an input like below...
{
"number":[
"+39XXXXXXXX",
"+34XXXXXXXX",
"+49XXXXXXXX"
],
"message":"Sample msg..."
}
I handle it with a foreach loop—like so:
foreach ($message->number as $key => $number) {
...
}
However when I have an input like this:
{
"number": "+49XXXXXXXX",
"message": "Sample msg..."
}
I receive an error, cause there is no array to be looped inside the object.
So what is a good and efficient way to detect for this?
You can check if the var value is array using the is_array function:
if (is_array($message->number) {
foreach ($message->number as $key => $number) {
...
}
} else {
...
}
This question already has answers here:
Read Json/Array string in Php
(2 answers)
Closed 8 years ago.
In fact Im having some troubles with php script that i have made recently. The problem is that I can't get data from a json file damo.json using php. This is the code of the json file:
{ "checkouts":[
{
"billing_address":{
"country":"Italy",
"first_name":"christian"
}
},
{
"billing_address":{
"country":"Italy",
"first_name":"christian"
}
}
]
}
i want to get the first_name record. Is it possible using php ?
this is the php code:
<?php
$data = file_get_contents('demo.json');
$obj = json_decode($data);
foreach ($obj->billing_address as $result)
{
echo $result->name;
}
?>
After you fix your syntax as noted above load the file and use json_decode with first parameter the json and second parameter true for associative array.
$data = file_get_contents( "demo.json" );
$data = json_decode($data, true);
var_dump($data);//you have assoc array