I have an output from an API, consisting of a list of elements like the following :
{
"data": [
{
"url": "https://parktheater.yesplan.nl/api/event/7036825857-1426512948",
"id": "7036825857-1426512948",
"owner": {
"url": "https://parktheater.yesplan.nl/api/user/365891073-1365159547",
"id": "365891073-1365159547",
"name": "Klaas Seelen"
},
"name": "Ernst, Bobbie en de rest",
"group": {
"url": "https://parktheater.yesplan.nl/api/group/7036832513-1426512948",
"id": "7036832513-1426512948",
"name": "Wij willen water show",
"_type": "group"
},
"starttime": "2016-05-01T16:00:00+02:00",
"endtime": "2016-05-01T18:00:00+02:00",
"locations": [
{
"url": "https://parktheater.yesplan.nl/api/location/383044097-1365160415",
"id": "383044097-1365160415",
"name": "Grote Zaal",
"_type": "location"
}
]
}
This data is provided to me as a string of JSON encoded data, which I stored in a variable $nextevents.
I would like to filter things out, to keep only certain properties of each record :
Name ("ernst, bobbie en de rest")
Group name ("Wij willen water show")
Location type ("Grote zaal")
starttime ("2016-05-01T16:00:00+02:00")
How do I manage that?
Here is how to filter the data :
$json_data = json_decode($nextevents);
$filtered_data = [];
for($i = 0; $i < count($json_data); $i++){
$item = $json_data[$i];
$filtered_data[$i] = array(
"name" => $item["name"],
"group_name" => $item["group"]["name"],
"location_name" => $item["locations"][0]["name"],
"start_time" => $item["starttime"]
);
}
And then you can use it. For example, if you want to output it in a way similar to your example :
for($i = 0; $i < count($filtered_data); $i++){
$item = $filtered_data[$i];
echo "Name : ".$item["name"].", ".
"Group name : ".$item["group_name"].", ".
"Location : ".$item["location_name"].", ".
"Start time : ".$item["start_time"].
"\n");
}
I think the general idea is that to filter the data, the most practical way is to rebuild another object, keeping only what you want.
Related
I am trying to parse a xml to a nested json structure with php.
This is my test script:
$json_drives = array();
foreach($drives->DR as $dr){
$current_drive = array();
$current_drive['id'] = $dr->ID;
$current_drive['name'] = $dr->NAME->D;
$json_drives[] = $current_drive;
}
echo("Finished");
// Parse and save
$f = json_encode($json_drives);
file_put_contents('test12345.json', $f);
I get a structure like that:
[
{
"id": {
"0": "1"
},
"name": {
"0": "Name 1"
}
},
{
"id": {
"0": "2"
},
"name": {
"0": "Name 2"
}
},
// ...
]
But I dont want the keys "id" and "name" to be nested. It should look like this:
[
{
"id": "1"
"name": "Name 1"
},
{
"id": "2"
"name": "Name 2"
},
// ...
]
How can I handle that?
Assuming your JSON's "drive" objects will always have this structure:
"id": {
"0": "Some ID"
},
"name": {
"0": "Some name"
}
You can use:
$current_drive['id'] = ((array) $dr->ID)[0];
$current_drive['name'] = ((array) $dr->NAME->D)[0];
I need to decode a json file and display it in the page..
My code is not working, and i don't know whats wrong.
The output is just a blank page.
Here is the json file..
{
"data": [
{
"name": "Jhaimee",
"uid": 10000
},
{
"name": "Ally",
"uid": 10000133
},
{
"name": "Macers",
"uid": 1000056
},
{
"name": "Diego",
"uid": 100004
},
{
"name": "Killersmile",
"uid": 1000050
},
{
"name": "Joel",
"uid": 1000011
}
]
}
I want the output to be..
Jhaimee
Ally
Macers
Diego
Killersmile
Joel
Here is my php.
$data = file_get_contents("https://graph.facebook.com/fql?q=SELECT name,uid FROM user WHERE uid IN (SELECT recipients FROM thread WHERE folder_id = 0 ORDER BY message_count DESC) AND uid != me() LIMIT 10&access_token=xxxx");
$data = json_decode($data, true);
echo $data[0]["name"];
The outermost array key should be data
echo $data['data'][0]['name']
foreach($data['data'] as $person){ echo $person['name'] . "<br />\n"; }
I get this JSON after running query of particular customer sale history.
$output=
[
{
"customerID": 52970,
"sale": [
{
"item": "pencil",
}
],
"saleNumber": "25",
},
{
"customerID": 52970,
"sale": [
{
"item": "book",
}
],
"saleNumber": "26",
},
{
"customerID": 52970,
"sale": [
{
"item": "pen",
}
],
"saleNumber": "27",
}
]
when it comes to retrieving data such as customerID, I json decode($obj = json_decode($output)) and get the customerID as $ID = $obj->{'customerID'};
How to get the maximum saleNumber from this JSON
You could simply loop through the JSON and compare it. E.g.
$max = 0;
for($i = 0; $i < count($obj); $i++)
{
if((int)$obj[$i]->{"saleNumber"} > (int)$max)
$max = (int)$obj[$i]->{"saleNumber"};
}
// The max value should be in $max
EDIT
Also if saleNumber is the last element in the object, you shouldn't have a ','
Rather new to using json with php, bit of a last resort having searched over the net quite a bit already. I have this example ison file below, which I wish to be able to echo the total number of 'name:' fields within it so eg. 4 in the example below.
Question: How would I go about doing this?
[
{
"age": "22",
"name": "Dave"
},
{
"age": "21",
"name": "Alan"
},
{
"age": "19",
"name": "Luke"
},
{
"age": "30",
"name": "Nina"
}
]
If there will always be a name in each array then just:
echo count(json_decode($json, true));
If name may or not be present in each array then:
PHP >= 5.5.0:
echo count(array_column(json_decode($json, true), 'name'));
PHP < 5.5.0:
$count = 0;
foreach(json_decode($json, true) as $k => $v) {
isset($v['name']) ? $count++ : $count;
}
echo $count;
This is the JSON response that I'm getting from database. I want to print these data. For now, there's only 2 entries in my table. So the length of JSON should be 2. As data increases, count has to get increase. SO for showing output, I use a for loop. And I used count() for limiting the iteration of loop only once through the JSON.
MY JSON:
{
"log": [
{
"action": "qq",
"id": "1",
"Time": "2014-05-19T15:40:06+05:30",
"user": {
"firstName": "dani",
"type": {
"zzs": "1",
"typename": "lolo",
"id": "1",
"zzt": "1",
"zzu": "1",
"zzv": "1",
"zzw": "1",
"zzx": "1"
},
"id": "1",
"lastName": "fed",
"password": "lmfao",
"userName": "fyi"
},
"userIpAddress": "101.15.23.45"
},
{
"action": "vv",
"id": "2",
"Time": "2014-05-20T10:16:33+05:30",
"user": {
"firstName": "dani",
"type": {
"zzs": "1",
"typename": "lolo",
"id": "1",
"zzt": "1",
"zzu": "1",
"zzv": "1",
"zzw": "1",
"zzx": "1"
},
"id": "1",
"lastName": "web",
"password": "rolf",
"userName": "asap"
},
"userIpAddress": "192.168.0.181"
}
]
}
MY PHP
$out = json_decode($json_data, true);
$x= count($out);
echo $x;
The value that I get is 1 instead of 2. And as you can see I have an associative array. I was trying to print those datas.
for($i=0; $i<$x; $i++)
{
echo $out['action'];
echo $out['user'][$i]['firstName'] ;
echo $out['user']['type'][$i]['typename'] ;
}
I don't get output. HELP???
I think it may be counting just the "Log"... technically that response is 2 dimensional:
data[0] = log
data[0][0] = log.firstRecord
data[0][1] = log.secondRecord
Try iterating through the second dimension
you have 2 element inside log key in your array.
try this:
$x= count($out['log']);
this will count 2
demo
for your loop you should try like this:
$out = json_decode($json_data, true);
$x= count($out['log']);
$out = $out['log'];
for($i=0; $i<$x; $i++)
{
echo $out[$i]['action'];
echo $out[$i]['user']['firstName'] ;
echo $out[$i]['user']['type']['typename'] ;
}
complete demo