How to call json if key is number [duplicate] - php

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);

Related

How do I handle this Nested JSON in PHP as below? [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 10 months ago.
I've a GET request in PHP that receives data in JSON as below.
{
"data": {
"id": "1cc58ad2-ccfd-4ede-a6a6-35809c81cb8a",
"type": "payment",
"attributes": {
"status": "Sent",
"created_at": "2022-05-02T08:57:50.171+03:00",
"amount": {
"currency": "KES",
"value": "500.0"
},
"transfer_batches": [{
"status": "Sent",
"amount": "500.0",
"disbursements": [{
"amount": "500.0",
"status": "Transferred",
"origination_time": "2022-05-02T08:57:50.171+03:00",
"transaction_reference": "1651471070",
"destination_type": "till",
"destination_reference": "3eccf0c1-ab6b-41a4-b51a-4e8f588105f1"
}]
}],
"metadata": {
"something": "TST-01",
"something_else": "Something else"
},
"_links": {
"callback_url": "http://portal.zarafu.com/payments",
"self": "https://sandbox.kopokopo.com/api/v1/payments/1cc58ad2-ccfd-4ede-a6a6-35809c81cb8a"
}
}
}
}
How can I filter the transaction_reference and status.
I have tried..
$jsonData = json_decode($response);
echo '<pre>';
echo $jsonData->status;
echo '</pre>';
You can't get the status like that. You have to use the format of your json. Exemple for the status :
echo $jsonData->data->attributes->status;
For transaction reference, it's :
echo $jsonData->data->attributes->transfer_batches->0->disbursements->0->transaction_reference;
I put '0' because it's arrays

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

foreach loop through json values [duplicate]

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'];
}

Get data from JSON file with PHP (empty result with numbers)

I'm trying to get data from the following JSON file using PHP
I Want To Get All URls (link) ,
http://www.google.com
http://www.bing.com
Json
Here is what I tried
PHP
Thanks
You cannot access an object property using a number, it must be a string.
It is much easier to output json_decode as an array, and access properties that way. To do this, put true as the second parameter.
<?php
$json = '
{
"news": {
"name": "yahoo",
"url": "https://yahoo.com"
},
"links": [
{
"id": "1",
"url": "https://google.com"
},
{
"id": "2",
"url": "https://bing.com"
}
]
}';
$decode = json_decode($json, true);
echo $decode['links'][0]['url'];

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);

Categories