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 5 years ago.
I have a string that looks like this:
[
{
"id":"2",
"price":"39.99",
"timeStamp":"1506264307167",
"quantity":"1",
"colours":"Green",
"pid":"234234234"
},
{
"id":"2",
"price":"39.99",
"timeStamp":"1506264311757",
"quantity":"1",
"colours":"Blue",
"pid":"234234234"
}
]
I need to get the id from this JSON string using PHP.
So I tried this:
$details = '[
{
"id":"2",
"price":"39.99",
"timeStamp":"1506264307167",
"quantity":"1",
"colours":"Green",
"pid":"234234234"
},
{
"id":"2",
"price":"39.99",
"timeStamp":"1506264311757",
"quantity":"1",
"colours":"Blue",
"pid":"234234234"
}
]';
$details = json_encode($details, true);
$array = json_decode($details, true);
$oid = $array['id'];
echo $oid;
The code above is in a while loop so the echo $oid should echo the id
multiple times.
anyway, the code above only prints this:
[
[
and when i look in the error log, i see this error:
PHP Warning: Illegal string offset 'id'
Could someone please advice on this issue?
Thanks in advance.
<?php
$details = '[
{
"id":"2",
"price":"39.99",
"timeStamp":"1506264307167",
"quantity":"1",
"colours":"Green",
"pid":"234234234"
},
{
"id":"2",
"price":"39.99",
"timeStamp":"1506264311757",
"quantity":"1",
"colours":"Blue",
"pid":"234234234"
}
]';
$array = json_decode($details, true);
for($i=0;$i<count($array);$i++){
$idValue = $array[$i]['id'];
echo $idValue;
}
You need a loop to go through all the nested arrays and get all the ids.
Try the following code:
<?php
$details = '[
{
"id":"2",
"price":"39.99",
"timeStamp":"1506264307167",
"quantity":"1",
"colours":"Green",
"pid":"234234234"
},
{
"id":"2",
"price":"39.99",
"timeStamp":"1506264311757",
"quantity":"1",
"colours":"Blue",
"pid":"234234234"
}
]';
$array = json_decode($details, true);
$oid = $array[0]['id'];
echo $oid;
Instead of encoding json string into json. Decode it to PHP array directly. And use [0] index to get first Json Object
Related
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
I am writing a php file for an application that allows the user to print a project report (with fpdf library).
The aim is to get datas from a db and then to build the PDF file dynamically.
The datas are stored in json.
It is ok for almost all datas, but there are some that I can't reach.
Here is an example :
First of all I already did this :
$Array['roof_coordinates'] = json_decode($Array['roof_coordinates'], false);
To get the number "nb" of "A523500" I'm doing like that :
$Array['roof_coordinates']->results->packingList->total->A523500->nb;
What am I doing wrong ?
"roofCoordinates": {
"results": {
"packingList": {
"total": {
"A523500": {
"ref": "STRA523500",
"nb": 16
},
"A523120": {
"ref": "STRA523120",
"nb": 0
},
"A522100": {
"ref": "STRA522100",
"nb": 8
},
},
}
},
}
And I tried to pass "true" to json_decode to convert objects to associative array but it doesn't seems to work...
Any help will be great !
Thank you in advance ;)
Make sure you are accessing the resulting data structure correctly, it should work whether you decode to an array or an object.
<?php
$json = <<<END
{
"roofCoordinates": {
"results": {
"packingList": {
"total": {
"A523500": {
"ref": "STRA523500",
"nb": 16
},
"A523120": {
"ref": "STRA523120",
"nb": 0
},
"A522100": {
"ref": "STRA522100",
"nb": 8
}
}
}
}
}
}
END;
$arrayResults = json_decode($json, true);
$nbFromArray = $arrayResults['roofCoordinates']['results']['packingList']['total']['A523500']['nb'];
$stdClassResults = json_decode($json);
$nbFromStdClass = $stdClassResults->roofCoordinates->results->packingList->total->A523500->nb;
assert($nbFromArray==16, 'Value should equal 16');
assert($nbFromArray==$nbFromStdClass, 'Values from either json_decode method should be equal');
echo 'From array: '.$nbFromArray.PHP_EOL;
echo 'From stdClass: '.$nbFromStdClass.PHP_EOL;
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);
I'm trying to get the IDs of the steam groups someone is connected to. Here is the json output:
{
"response": {
"success": true,
"groups": [
{
"gid": "111"
},
{
"gid": "222"
},
{
"gid": "333"
},
{
"gid": "444"
},
{
"gid": "555"
}
]
}
}
I've attempted it via:
$groupIDs = $reply['response']['groups'];
foreach ($groupIDs as $gID) {
// Do stuff
}
I'm getting the following error, but I'm struggling to see how to correct it.
Invalid argument supplied for foreach()
Sorry I didn't make it clear. I'm already decoding it before the foreach().
$reply = json_decode($reply, true);
First you have to decode the json string using php function json_decode. Then iterate the object as shown below
$string = '{
"response": {
"success": true,
"groups": [
{
"gid": "111"
},
{
"gid": "222"
},
{
"gid": "333"
},
{
"gid": "444"
},
{
"gid": "555"
}
]
}
}';
$array = json_decode($string);
foreach($array->response->groups as $value ){
echo $value->gid;
echo "<br/>";
}
http://php.net/manual/pt_BR/function.json-decode.php
Try:
$response = json_decode($reply, true);
$groupIDs = $response['response']['groups'];
foreach ($groupIDs as $gID) {
// Do stuff
}
use json_decode($response) cf doc
$rep = json_decode($response)
foreach ($rep->response->groups as $gID) {
// Do stuff
}
$groups = $reply['response']->groups;
foreach ($groups as $group) {
print $group->gid;
}
In json each {} means is parsed as object, each [] is parsed as array.
This question already has answers here:
how to add item to the json file formatted array
(5 answers)
Closed 7 years ago.
I have this JSON array, and i want to add another value to it using PHP.
What would be the easiest way to add a ID and Name to this array using PHP.
[
{
"id":1,
"name":"Charlie"
},
{
"id":2,
"name":"Brown"
},
{
"id":3,
"name":"Subitem",
"children":[
{
"id":4,
"name":"Alfa"
},
{
"id":5,
"name":"Bravo"
}
]
},
{
"id":8,
"name":"James"
}
]
Simply, decode it using json_decode()
And append array to resulting array.
Again encode it using json_encode()
Complete code:
<?php
$arr = '[
{
"id":1,
"name":"Charlie"
},
{
"id":2,
"name":"Brown"
},
{
"id":3,
"name":"Subitem",
"children":[
{
"id":4,
"name":"Alfa"
},
{
"id":5,
"name":"Bravo"
}
]
},
{
"id":8,
"name":"James"
}
]';
$arr = json_decode($arr, TRUE);
$arr[] = ['id' => '9999', 'name' => 'Name'];
$json = json_encode($arr);
echo '<pre>';
print_r($json);
echo '</pre>';