foreach loop through json values [duplicate] - 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 have a json result from an URL:
{
"result": [{
"user.name": "Spider Man",
"user": "a4ac7bfe6f581640a62d3c31be3ee4dc"
}, {
"user.name": "Bat Man",
"user": "af406b85e4b13500b95fa1eeac1ce626"
}, {
"user.name": "Iron Man",
"user": "18ed9aba4ffb07006979ab6ba110c757"
}, {
"user.name": "Ant Man",
"user": "877a503a98cc4200b95f526ea1ece471"
}, {
"user.name": "Captain America",
"user": "8ec0d9634f2f22004b19ca1f0310c791"
}]
}
How to create a foreach loop to get the following output:
Spider Man
Bat Man
Iron Man
Ant Man
Captain America
And the same for the user-values in the json.
I tried following code but I can't figure it out:
$json = file_get_contents($queryURL, false, $context) or die ("keine verbindung");
$array = json_decode($json, true);
foreach($array as $data) {
$data->user;
}
Thanks for your help.

You decoded as an array not an object. Also, there is another level result:
foreach($array['result'] as $data) {
echo $data['user'];
}

Related

PHP get JSON key value from specific object

Ive got the following JSON:
{
"servers": [
{
"id": "f34c0185-4c9e-40fd-82f6-1d6e9a5d499e",
"name": "vm01"
},
{
"id": "d671ac7d-3b5a-4777-8510-6e8e58295061",
"name": "vm02"
},
{
"id": "h59j23cc-9ve2-4508-1277-85y1lo27562m",
"name": "vm03"
}
]
}
I also have another JSON that gives me the ID I want to search for.
For example: "d671ac7d-3b5a-4777-8510-6e8e58295061".
I want to search for the JSON Object, that contains that ID and get the value of the name key. I tried with loops and if, else's but I didn't manage to get it working.
Thanks for your help!
decode the json as array object then loop through with the ID that u want to search
<?php
$json = '{
"servers": [
{
"id": "f34c0185-4c9e-40fd-82f6-1d6e9a5d499e",
"name": "vm01"
},
{
"id": "d671ac7d-3b5a-4777-8510-6e8e58295061",
"name": "vm02"
},
{
"id": "h59j23cc-9ve2-4508-1277-85y1lo27562m",
"name": "vm03"
}
]
}';
$j = json_decode($json, true);
foreach($j['servers'] as $arr)
{
if( $arr['id'] == 'd671ac7d-3b5a-4777-8510-6e8e58295061' ) echo $arr['name'];
}
demo: https://3v4l.org/0DboX

How to call json if key is number [duplicate]

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 json data like this decode from API, i get with PHP
{
"0": {
"id_siswa": "14477",
"rombel": "15",
"nama_lengkap": "Cahyo"
},
"1": {
"id_siswa": "14484",
"rombel": "15",
"nama_lengkap": "Bowo"
},
"2": {
"id_siswa": "14485",
"rombel": "13",
"nama_lengkap": "Agus Sugiharto"
}
}
but when i call
$data[0]->id_siswa
Uncaught Error: Cannot use object of type stdClass as array
then i try to call like
$data->id_siswa
error : Undefined property: stdClass::$id_siswa in
You need to decode json to get array value. you will get id_siswa output.
$json = '{
"0": {
"id_siswa": "14477",
"rombel": "15",
"nama_lengkap": "Cahyo"
},
"1": {
"id_siswa": "14484",
"rombel": "15",
"nama_lengkap": "Bowo"
},
"2": {
"id_siswa": "14485",
"rombel": "13",
"nama_lengkap": "Agus Sugiharto"
}
}';
echo "<pre>";
$result = json_decode($json,true);
print_r($result);
echo $result[0]['id_siswa'];
you can use json_decode to convert json into array and get the corresponding values with $data[0]['id_siswa'] like this.
$result = json_decode($json,true);

Looping through a json file to find a particular value in PHP

My json file look likes
myjson.json
[
{"name":"category_01","data":
[
{"id":"1","word":"ma","given_value":"1"},
{"id":"3","word":"me","given_value":"1"},
] }
[
{"name":"category_02","data":
[
{"id":"1","word":"vea","given_value":"1"},
{"id":"3","word":"ve","given_value":"1"},
] }
So what I want here is, check whether a particular value is in this json array using php. Assume that,
myphp.php
$word = 've';
if this value is in the above array, should find is it in category_01 or category_02. and also want to find given_value of matching word.
I just tried in this way,
$data = file_get_contents ("myjson.json");
$json = json_decode($data, true);
foreach($arr as $item) {
$uses = ($item['word']= $word);
}
This doesn't work. How can I fix this, Please help me!
First of all, the JSON you posted is invalid. I think it should look like this:
[
{
"name": "category_01",
"data": [{
"id": "1",
"word": "ma",
"given_value": "1"
},
{
"id": "3",
"word": "me",
"given_value": "1"
}
]
},
{
"name": "category_02",
"data": [{
"id": "1",
"word": "vea",
"given_value": "1"
},
{
"id": "3",
"word": "ve",
"given_value": "1"
}
]
}
]
Try using on online tool like https://jsonlint.com/ to check your JSON. (if you get errors i recommend build the json again from scratch).
I also recommend checking your json before using it:
if ($arr === null && json_last_error() !== JSON_ERROR_NONE) {
die("incorrect json data");
}
To get your value you have to KNOW how your data looks like and then process it:
foreach($arr as $category) {
foreach($category['data'] as $data) {
if(strstr($data['word'], $word))
echo $category['name'].' '.$data['word'].' '.$data['given_value']."\n";
}
}

Parsing Arrays in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parsing JSON file with PHP
Can anyone suggest an elegant way to change an array of the form below to an array containing only the primary keys, using php?
[{
"PrimaryKey": "489",
"name": "Ted"
}, {
"PrimaryKey": "488",
"name": "Bill"
}, {
"PrimaryKey": "487",
"singleFbId": "Joe"
}]
<?php
$data = '[{
"PrimaryKey": "489",
"name": "Ted"
}, {
"PrimaryKey": "488",
"name": "Bill"
}, {
"PrimaryKey": "487",
"singleFbId": "Joe"
}]';
$array = json_decode($data, TRUE);
print_r($array);

Help with php loop

i need to extract data from this array of objects
{
"data": [
{
"id": "136104923104306",
"from": {
"name": "GetWith.It",
"category": "Website",
"id": "136132969751208"
},
"message": "Do u know y **LOVE IS BLIND**\nbcoz..\n''ur mom started to love u before seeing ur face''....!",
"updated_time": "2010-10-05T13:41:42+0000",
"comments": {
"data": [
{
"id": "136104923104306_1075253",
"from": {
"name": "Hressence Ec",
"id": "1464305271"
},
"message": "this I would agree..love is surely blind..",
"created_time": "2010-10-12T01:40:47+0000",
}
]
}
}
My current code:
$data=json_decode(file_get_contents('https://myurl/where/this/data/is'));
foreach($data as $dts){
echo "$dts->message";
};
i need to extract the comments ..
and when i try
foreach($data->comments->data as $dts){
echo "$dts->message";
};
it returns null!
help please
Your $data is actually an object with a data property that is an array containing another object with the comments object you are looking for. So:
foreach ($data->data as $item) {
foreach ($item->comments->data as $comment) {
echo $comment->message;
}
}
I suggest you transform it into an associative array:
$data=json_decode(file_get_contents('https://myurl/where/this/data/is'), true);
and have a look at the structure.
I think your loop must look like this:
foreach($data['data'] as $dts) {
echo $dts['message'];
}
Update: Although I was able to fix the JSON (at least it seems to be good now) json_decode still returns null. No clue way. First make sure your JSON string is valid!

Categories