I am really struggling to create a new array from this response, I don't know where and how to start or to do it:
{
"fruits": [
{
"name": "Bananas",
"pieces": 2469429
},
{
"name": "Oranges",
"pieces": 1576890
},
{
"name": "Lemons",
"pieces": 5645318
}
],
"size": [
{
"size": "5000-10,000 eaters",
"pieces": 17008533
},
{
"size": "100-500 eaters",
"pieces": 23473914
},
{
"size": "10,001+ eaters",
"pieces": 68086139
}
],
"SizeAndFruit": [
{
"size": "100-500 eaters",
"pieces": 226636,
"name": "Bananas"
},
{
"size": "5000-10,000 eaters",
"pieces": 249004,
"name": "Bananas"
},
{
"size": "10,001+ eaters",
"pieces": 727829,
"name": "Bananas"
},
{
"size": "100-500 eaters",
"pieces": 416310,
"name": "Lemons"
},
{
"size": "5000-10,000 eaters",
"pieces": 488711,
"name": "Lemons"
},
{
"size": "10,001+ eaters",
"pieces": 2340652,
"name": "Lemons"
},
{
"size": "100-500 eaters",
"pieces": 217037,
"name": "Oranges"
},
{
"size": "5000-10,000 eaters",
"pieces": 71779,
"name": "Oranges"
},
{
"size": "10,001+ eaters",
"pieces": 201729,
"name": "Oranges"
}
]
}
I want to get this new structure/array:
{
"newArray": [
{
"name": "Bananas",
"totalResults": 2469429,
"sortedResults": [
{
"size": "100-500 eaters",
"pieces": 226636
},
{
"size": "5000-10,000 eaters",
"pieces": 249004
},
{
"size": "10,001+ eaters",
"pieces": 727829
}
]
},
{
"name": "Oranges",
"totalResults": 1576890,
"sortedResults": [
{
"size": "100-500 eaters",
"pieces": 217037
},
{
"size": "5000-10,000 eaters",
"pieces": 71779
},
{
"size": "10,001+ eaters",
"pieces": 201729
}
]
},
{
"name": "Lemons",
"totalResults": 5645318,
"sortedResults": [
{
"size": "100-500 eaters",
"pieces": 416310
},
{
"size": "5000-10,000 eaters",
"pieces": 488711
},
{
"size": "10,001+ eaters",
"pieces": 2340652
}
]
}
]
}
How can I make for each fruit to add all sizes as subarray from the sizeAndFruit as the example above? After that each fruit has to be row in a table, that's why I need to get this new array? Please let me know if you have any suggestions or any extra questions I will be glad to explain more if needed. Thank you very much!
Edit for David:
How about if we always order them by size?
Here is the snippet: pastecode.io/s/t82n19x4 so when the order is like this. So, to order them by size as it is now on David's reply, because if these are mixed then they order as they go instead by 100-500 5001-10000 10,000+ they go 10.000+ 100-500 5001-1000, etc.
Thank you!
The below codes reads your inputs and turns it into your desired output (including sorting by size):
<?php
$inputContents = file_get_contents(__DIR__ . '/input.json');
$inputJson = json_decode($inputContents);
$out = [];
foreach ($inputJson->fruits as $fruit) {
$out[] = [
'name' => $fruit->name,
'totalResults' => $fruit->pieces,
'sortedResults' => [],
];
}
foreach ($inputJson->SizeAndFruit as $entry) {
$newEntry = [
'size' => $entry->size,
'pieces' => $entry->pieces,
];
foreach ($out as &$fruit) {
if ($fruit['name'] === $entry->name) {
$fruit['sortedResults'][] = $newEntry;
}
}
}
/**
* Get a number of the size from the provided string.
*
* #param string $size
*
* #return int|null
*/
function sizeStringToNumber(string $size): ?int
{
$sizes = [
'100-500 eaters' => 100,
'5000-10,000 eaters' => 5000,
'10,001+ eaters' => 10000,
];
if (isset($sizes[$size])) {
return $sizes[$size];
}
throw new \Exception('Unknown size: ' . $size);
}
/**
* Sort the `sortedResults` array by `size` in ascending order.
*/
foreach ($out as &$fruit) {
usort($fruit['sortedResults'], function ($a, $b) {
$aSize = sizeStringToNumber($a['size']);
$bSize = sizeStringToNumber($b['size']);
return $aSize <=> $bSize;
});
}
$outArray = [
'newArray' => $out,
];
$outJson = json_encode($outArray, JSON_PRETTY_PRINT);
print_r($outJson);
Prints as:
{
"newArray": [
{
"name": "Bananas",
"totalResults": 2469429,
"sortedResults": [
{
"size": "100-500 eaters",
"pieces": 226636
},
{
"size": "5000-10,000 eaters",
"pieces": 249004
},
{
"size": "10,001+ eaters",
"pieces": 727829
}
]
},
{
"name": "Oranges",
"totalResults": 1576890,
"sortedResults": [
{
"size": "100-500 eaters",
"pieces": 217037
},
{
"size": "5000-10,000 eaters",
"pieces": 71779
},
{
"size": "10,001+ eaters",
"pieces": 201729
}
]
},
{
"name": "Lemons",
"totalResults": 5645318,
"sortedResults": [
{
"size": "100-500 eaters",
"pieces": 416310
},
{
"size": "5000-10,000 eaters",
"pieces": 488711
},
{
"size": "10,001+ eaters",
"pieces": 2340652
}
]
}
]
}
Related
{
"currency": "USD",
"results": [
{
"itineraries": [
{
"outbound": {
"duration": "08:35",
"flights": [
{
"departs_at": "2018-07-03T18:35",
"arrives_at": "2018-07-03T21:15",
"origin": {
"airport": "BOS",
"terminal": "A"
},
"destination": {
"airport": "YHZ"
},
"marketing_airline": "WS",
"operating_airline": "WS",
"flight_number": "3713",
"aircraft": "DH4",
"booking_info": {
"travel_class": "ECONOMY",
"booking_code": "M",
"seats_remaining": 5
}
},
{
"departs_at": "2018-07-03T22:20",
"arrives_at": "2018-07-04T08:10",
"origin": {
"airport": "YHZ"
},
"destination": {
"airport": "LGW",
"terminal": "N"
},
"marketing_airline": "WS",
"operating_airline": "WS",
"flight_number": "24",
"aircraft": "7M8",
"booking_info": {
"travel_class": "ECONOMY",
"booking_code": "M",
"seats_remaining": 1
}
}
]
}
}
],
"fare": {
"total_price": "653.40",
"price_per_adult": {
"total_fare": "653.40",
"tax": "178.40"
},
"restrictions": {
"refundable": true,
"change_penalties": true
}
}
},
This is json data fetched from live flights query. I want to be able to determine the number of stops for each flight using the [depart_at] field. So I have been struggling to get the number of occurrences of the [depart_at] field so as to derive how many stops(layovers) for each flight. I have already converted to php using the code below: But I am not getting the count.
<?php foreach ($itinerary['outbound']['flights'] as $flight ){
echo count($flight['flights']);
}?>
You can try like this:
$json = '{
"currency": "USD",
"results": [
{
"itineraries": [
{
"outbound": {
"duration": "08:35",
"flights": [
{
"departs_at": "2018-07-03T18:35",
"arrives_at": "2018-07-03T21:15",
"origin": {
"airport": "BOS",
"terminal": "A"
},
"destination": {
"airport": "YHZ"
},
"marketing_airline": "WS",
"operating_airline": "WS",
"flight_number": "3713",
"aircraft": "DH4",
"booking_info": {
"travel_class": "ECONOMY",
"booking_code": "M",
"seats_remaining": 5
}
},
{
"departs_at": "2018-07-03T22:20",
"arrives_at": "2018-07-04T08:10",
"origin": {
"airport": "YHZ"
},
"destination": {
"airport": "LGW",
"terminal": "N"
},
"marketing_airline": "WS",
"operating_airline": "WS",
"flight_number": "24",
"aircraft": "7M8",
"booking_info": {
"travel_class": "ECONOMY",
"booking_code": "M",
"seats_remaining": 1
}
}
]
}
}
],
"fare": {
"total_price": "653.40",
"price_per_adult": {
"total_fare": "653.40",
"tax": "178.40"
},
"restrictions": {
"refundable": true,
"change_penalties": true
}
}
}';
echo substr_count($json, 'departs_at');
You can try that way.
$countDepartsAt = 0;
foreach ($itinerary['outbound']['flights'] as $flight ){
if(array_key_exists('departs_at', $flight)) {
$countDepartsAt++;
}
}
echo $countDepartsAt;
can you help me please? I have a ecommerce website with 1000+ products. Each product has a bunch of options like "color", "size", and other specs... but i don't know all the attributes. so i define a document with this mapping:
"mappings" : {
"article" : {
"properties": {
"options": {
"type": "nested",
"include_in_parent":"true",
"properties": {
"id": {"type": "string"},
"name": {"type": "string"},
"values": {"type": "string"}
}
}
}
}
And this is my Query to get the Bucket list:
{
"query": {
"bool": {
"must": [
{
"term": {
"categorie_id": "f52330ce2669dfab884c2d60468b8466"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 1,
"sort": [
{
"sorttype": {
"order": "desc"
}
},
"_score"
],
"aggs": {
"baked_goods": {
"nested": {
"path": "options"
},
"aggs": {
"name": {
"terms": {
"field": "id"
},
"aggs": {
"name": {
"terms": {
"field": "values"
}
}
}
}
}
}
}
}
I get Documents, but the Result of the Buckets is Empty...
"aggregations": {
"baked_goods": {
"doc_count": 3331,
"name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [ ]
}
}
}
i want something like:
"color" => "red" (4)
"color" => "blue" (2)
"size" => "X" (11)
..
Can you please help me??
i found a solution.
Mapping:
"options": {
"type": "nested",
"include_in_parent": true,
"properties": {
"name": { "type": "text" , "analyzer": "whitespace", "fielddata": true},
"values": { "type": "text" , "analyzer": "whitespace", "fielddata": true}
}
}
Query:
"aggs": {
"facets": {
"nested": {
"path": "options"
},
"aggs": {
"name": {
"terms": {
"field": "options.name"
},
"aggs": {
"name": {
"terms": {
"field": "options.values"
}
}
}
}
}
} }
Newbie here, I'm trying to get facebook name of user who left comment on my facebook page post but its not working. I'm using graph API v2.6. Here is my code:
$data = $this->check_file_get_contents($this->graph_api.$object_id.'/comments?
fields=from,name&access_token='.$access_token);
//$data = $this->check_file_get_contents($this->graph_api.$object_id.'/&access_token='.$access_token);
$res = json_decode($data);
$uname = $res->name;
echo $uname;
Any idea of what could be wrong here? Little guidance would be very helpful. What did I miss?
The $object_id is the Post ID. This is the output from graph api:
{
"data": [
{
"from": {
"name": "John Doe",
"id": "932242153456808"
},
"id": "585601208257495_620658141418468"
},
{
"from": {
"name": "Jason King",
"id": "10153078961444510"
},
"id": "585601208257495_622535501230732"
},
{
"from": {
"name": "Jason King",
"id": "10153078961444510"
},
"id": "585601208257495_622588581225424"
}
],
"paging": {
"cursors": {
"before": "WTI5dGJXVnVkRjlqZAFhKemIzSTZAOakl3TmpVNE1UUXhOREU0TkRZANE9qRTBOalEyTkRRMU5EZAz0ZD",
"after": "WTI5dGJXVnVkRjlqZAFhKemIzSTZAOakl5TlRnNE5UZA3hNakkxTkRJME9qRTBOalV3TnpVNE9Uaz0ZD"
}
}
}
Try this out
<?php
$json='{
"data": [
{
"from": {
"name": "John Doe",
"id": "932242153456808"
},
"id": "585601208257495_620658141418468"
},
{
"from": {
"name": "Jason King",
"id": "10153078961444510"
},
"id": "585601208257495_622535501230732"
},
{
"from": {
"name": "Jason King",
"id": "10153078961444510"
},
"id": "585601208257495_622588581225424"
}
],
"paging": {
"cursors": {
"before": "WTI5dGJXVnVkRjlqZAFhKemIzSTZAOakl3TmpVNE1UUXhOREU0TkRZANE9qRTBOalEyTkRRMU5EZAz0ZD",
"after": "WTI5dGJXVnVkRjlqZAFhKemIzSTZAOakl5TlRnNE5UZA3hNakkxTkRJME9qRTBOalV3TnpVNE9Uaz0ZD"
}
}
}';
$res=json_decode($json,true);
$data=$res['data'];
for ($x = 0; $x <= count($data)-1; $x++) {
echo $data[$x]['from']['name']."<br>\n";
}
?>
Output:
John Doe<br>
Jason King<br>
Jason King<br>
I am wanting to get some code to get a list of each unique client ID and how often that client ID is repeated through the array.
Below is a snippet of the JSON.
What I would like is something like the following please in PHP if possible.
Mac 12:12:12:12:12 20 times
Mac 23:23:23:23:23 15 times
Mac 34:34:34:34:34 2 times
Is there an easy way to do this please?
Thanks.
Rob
"ranges": [
{
"clients": [
{
"clientId": {
"mac": "86:8f:c2:8f:c3:20"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -90.4
}
]
},
{
"clientId": {
"mac": "6c:19:8f:bf:47:e9"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -91.3
}
]
},
{
"clientId": {
"mac": "58:6d:8f:75:95:0e"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -86.3
}
]
},
{
"clientId": {
"mac": "68:72:51:10:e7:26"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -53.7
}
]
},
{
"clientId": {
"mac": "38:2c:4a:5c:b6:a0"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -87.1
}
]
},
{
"clientId": {
"mac": "68:72:51:10:e7:29"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -76.9
}
]
},
{
"clientId": {
"uniqueId": "CQos"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -70.2
}
]
},
{
"clientId": {
"mac": "a4:ee:57:2e:ac:bd"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -95
}
]
},
{
"clientId": {
"uniqueId": "ECgg"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -75.4
}
]
},
{
"clientId": {
"mac": "58:6d:8f:74:bf:f9"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -70
}
]
}
],
"timestamp": "2015-11-09T22:06:00+00:00"
},
{
"clients": [
{
"clientId": {
"mac": "86:8f:c2:8f:c3:20"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -93
}
]
},
{
"clientId": {
"mac": "6c:19:8f:bf:47:e9"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -90.8
}
]
},
{
"clientId": {
"mac": "58:6d:8f:75:95:0e"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -87.2
}
]
},
{
"clientId": {
"mac": "68:72:51:10:e7:26"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -54.1
}
]
},
{
"clientId": {
"mac": "38:2c:4a:5c:b6:a0"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -87
}
]
},
{
"clientId": {
"mac": "68:72:51:10:e7:29"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -77.2
}
]
},
{
"clientId": {
"uniqueId": "CQos"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -70.8
}
]
},
{
"clientId": {
"mac": "a4:ee:57:2e:ac:bd"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -95
}
]
},
{
"clientId": {
"uniqueId": "ECgg"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -72.8
}
]
},
{
"clientId": {
"mac": "58:6d:8f:74:bf:f9"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -70
}
]
}
],
"timestamp": "2015-11-09T22:07:00+00:00"
},
First loop through the json and build an array of mac addresses:
$data = json_decode($json, true);
$macs = array();
foreach ($data['ranges'] as $range) {
foreach ($range['clients'] as $client) {
// check if the client has a mac address, and add it to the array
if (isset($client['clientId']['mac'])) {
$macs[] = $client['clientId']['mac'];
}
}
}
Then you can simply use array_count_values:
var_dump(array_count_values($macs));
This will output an array with the mac address as the key, and the frequency as the value:
array (size=8)
'86:8f:c2:8f:c3:20' => int 2
'6c:19:8f:bf:47:e9' => int 2
'58:6d:8f:75:95:0e' => int 2
'68:72:51:10:e7:26' => int 2
'38:2c:4a:5c:b6:a0' => int 2
'68:72:51:10:e7:29' => int 2
'a4:ee:57:2e:ac:bd' => int 2
'58:6d:8f:74:bf:f9' => int 2
So you can just do
foreach (array_count_values($macs) as $mac => $frequency)
{
echo "Mac {$mac} {$frequency} times<br/>";
}
There are other ways, for example in the loop you could check if the mac has been seen already, and just add 1 to a count as you go along. But this way seems simplest.
Not sure about an easy way, but you can iterate over the data and store your findings in an array.
//parse json
$data = json_decode( $json );
//store mac addresses
$parsed = array();
foreach( $data['ranges'] as $range ) {
foreach( $range->clients as $client ) {
$address = $client->clientId->mac;
if( ! isset( $parsed[$address] ) )
$parsed[$address] = 0;
$parsed[$address]++;
}
}
//output as requested
foreach( $parsed as $mac => $count )
echo "Mac {$mac} {$count} <br />";
I'm trying to make a json that looks like this:
{
"data": {
"error_message": "",
"data": {
"person": [
[
{
"name": "teset1",
"image": "http://test.co.il/test/icons/billadress.png"
},
{
"name": "test2",
"image": "http://test.co.il/test/icons/email.png"
},
{
"name": "test3",
"image": "http://test.co.il/test/icons/homeicon.png"
}
],
[
{
"name": "a",
"image": "http://test.co.il/shay/keykit/icons/billadress.png"
},
{
"name": "b",
"image": "http://test.co.il/shay/keykit/icons/email.png"
},
{
"name": "c",
"image": "http://dvns.co.il/shay/keykit/icons/homeicon.png"
}
],
[
{
"name": "a",
"image": "http://test.co.il/test/icons/billadress.png"
},
{
"name": "b",
"image": "http://test.co.il/test/icons/email.png"
},
{
"name": "c",
"image": "http://dvns.co.il/test/icons/homeicon.png"
}
],
]
}
},
}
This is the code i'm using after i'm getting the name & image from the mysql query:
$response = $this->_db->query($query)->result_array();
$icons_results = array();
$obj = new stdclass();
foreach ($response as $key => $response) {
$icons_model = new icons_model();
$icons_model->init($response);
$obj->families[] = $icons_model;
}
return $obj;
SO .. this is what i'm getting:
{
"data": {
"families": [
{
"id": "1",
"name": "a",
"image_url": "http://test.co.il/shay/keykit/icons/billadress.png"
},
{
"id": "2",
"name": "b",
"image_url": "http://test.co.il/shay/keykit/icons/email.png"
},
{
"id": "3",
"name": "c",
"image_url": "http://test.co.il/test/icons/homeicon.png"
},
{
"id": "6",
"name": "f",
"image_url": "http://test.co.il/test/icons/homeicon.png"
},
{
"id": "4",
"name": "d",
"image_url": "http://test.co.il/test/icons/billadress.png"
},
{
"id": "5",
"name": "e",
"image_url": "http://test.co.il/test/icons/billadres2323s.png"
},
and do on.
How to i make that every three object will be in a group? (like the first example)
Try this:
foreach ($response as $key => $response) {
$icons_model = new icons_model();
$icons_model->init($response);
$obj->families[(int)($key / 3)][] = $icons_model;
}