I am trying to get data from a multidimensional array I got from a Facebook response, into a simpler array.
the json data is as seen below.
{
"albums": {
"data": [
{
"photos": {
"data": [
{
"name": "a photo name",
"source": "https://fbcdn-sphotos-d-a.akamaihd.net/hphotos-ak-xlp1/t31.0-8/s720x720/XXXXXXXXXXXX",
"picture": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-0/XXXXXXXXXXXXXX",
},
{
"name": "a photo name",
"source": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xtp1/v/t1.0-0/p480x480/XXXXXXXXXXXXXXXXXX",
"picture": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xtp1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
},
{
"name": "a photo name",
"source": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xtf1/v/t1.0-0/p480x480/XXXXXXXXXXXXXX",
"picture": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xtf1/v/t1.0-0/s130x130/XXXXXXXXXXXXXXX",
},
{
"name": "a photo name",
"source": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-0/p480x480/XXXXXXXXXXXXXXXXXX",
"picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
}
],
"paging": {
"cursors": {
"before": "MTXXXXXXXXXXXXXXXZD",
"after": "MXXXXXXXXXXXXXZD"
}
}
},
},
{
"photos": {
"data": [
{
"source": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-9/XXXXXXXXXXXXXXX",
"picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
},
{
"name": "XXXXXXXXX",
"source": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/XXXXXXXXXXX",
"picture": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
},
{
"name": "XXXXXXXXXXX",
"source": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/XXXXXXXXXXX",
"picture": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-0/p130x130/XXXXXXXXXXX",
},
{
"name": "a photo name",
"source": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXXXXX",
"picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
},
{
"name": "a photo name",
"source": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xpa1/t31.0-0/XXXXXXXXXXX",
"picture": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xpt1/v/t1.0-0/XXXXXXXXXX",
},
{
"name": "a photo name",
"source": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xaf1/t31.0-0/p480x480/XXXXXXXXXX",
"picture": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xat1/v/t1.0-0/s130x130/XXXXXXXX",
},
{
"name": "a photo name",
"source": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX",
"picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/v/t1.0-0/s130x130/XXXXXXXX",
}
],
"paging": {
"cursors": {
"before": "MTXXXXXXXXXXXXXXXgZDXXXX",
"after": "MTcXXXXXXXXXXXXXXXXXD"
}
}
},
},
{
"photos": {
"data": [
{
"source": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xta1/v/t1.0-9/1",
"picture": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xta1/v/t1.0-0/s130x130/",
},
{
"name": "XXXXXXXXXXXXXXXXX",
"source": "https://scontent.xx.fbcdn.net/v/t1.0-9/11",
"picture": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xaf1/v/t1.0-0/s130x130/11",
}
],
"paging": {
"cursors": {
"before": "MOIXXXXXXXXXXXXX",
"after": "MTXXXXXXXXXXXXXD"
}
}
},
},
{
"photos": {
"data": [
{
"source": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xpl1/t31.0-8/s720x720/",
"picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xft1/v/t1.0-0/s130x130/",
}
],
"paging": {
"cursors": {
"before": "MTXXXXXXXXXXXSJKSDJZDZD",
"after": "MTdXXXXXXXXDKSJKDNXXXXXXXXXpZD"
}
}
},
}
],
"paging": {
"cursors": {
"before": "MXXXXXXXXXXXXXXD",
"after": "MXXXXXXXXXXXXJR"
}
}
},
"feed": {
"data": [
{
},
{
"attachments": {
"data": [
{
"media": {
"image": {
"height": 480,
"src": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xpf1/t31.0-8/s720x720/",
"width": 720
}
}
}
]
}
},
{
"attachments": {
"data": [
{
"media": {
"image": {
"height": 102,
"src": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xta1/v/t1.0-9/",
"width": 197
}
}
}
]
}
},
{
"attachments": {
"data": [
{
"media": {
"image": {
"height": 276,
"src": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xpl1/t31.0-8/s720x720/",
"width": 720
}
}
}
]
}
},
{
},
{
"attachments": {
"data": [
{
"description": "XXXXXXXXXXXXKE",
"media": {
"image": {
"height": 540,
"src": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/",
"width": 540
}
}
}
]
}
},
{
"attachments": {
"data": [
{
"description": "XXXXXXXXXXJ",
"media": {
"image": {
"height": 375,
"src": "https://scontent.xx.fbcdn.net/v/t1.0-9/",
"width": 500
}
}
}
]
}
},
{
"attachments": {
"data": [
{
"description": "XXXXXXXXXXXX",
"media": {
"image": {
"height": 540,
"src": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xaf1/v/t1.0-0/p180x540/",
"width": 720
}
}
}
]
}
},
{
"attachments": {
"data": [
{
"description": "XXXXXXXXXXXXp",
"media": {
"image": {
"height": 405,
"src": "https://scontent.xx.fbcdn.net/v/t1.0-9/s720x720/",
"width": 720
}
}
}
]
}
},
{
"attachments": {
"data": [
{
"media": {
"image": {
"height": 392,
"src": "https://scontent.xx.fbcdn.net/v/t1.0-9/",
"width": 626
}
}
}
]
}
},
{
"attachments": {
"data": [
{
"description": "XXXXXXXX",
"media": {
"image": {
"height": 255,
"src": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/",
"width": 208
}
}
}
]
}
}
],
"paging": {
"previous": "https://graph.facebook.com/v2.5/1XXXXXXXXXXXX6/feed?fields=a",
"next": "https://graph.facebook.com/v2.5/1SXXXXXXXXXXXXX96/feed?fields="
}
},
}
I want to end up with an array like this:
Array
(
[photo] => Array
(
[0] => Array
(
[description] => a photo name
[image_src] => https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
)
[1] => Array
(
[description] => a photo name
[image_src] => https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
)
[2] => Array
(
[description] => a photo name
[image_src] => https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
)
and so on......
)
)
But I get this:
Array
(
[photo] => Array
(
[0] => Array
(
[description] => a photo name
)
[1] => Array
(
[image_src] => https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
)
[2] => Array
(
[description] => a photo name
)
[3] => Array
(
[image_src] => https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
)
and so on...
)
)
This is the code for my current loop:
foreach ($raw_facebook['albums']['data'] as $photos) {
if ($photos) {
foreach ($photos['photos']['data'] as $photo) {
if ( (isset($photo['name']) && array_key_exists('name', $photo)) && (isset($photo['source']) && $this->array_key_exists_recursive('source', $photo)) ) {
$facebook['photo'][]['description'] = $photo['name'];
$facebook['photo'][]['image_src'] = $photo['source'];
}
}
}
}
When I modify this code to this:
foreach ($raw_facebook['albums']['data'] as $photos) {
if ($photos) {
$i = 0;
foreach ($photos['photos']['data'] as $photo) {
if ( (isset($photo['name']) && array_key_exists('name', $photo)) && (isset($photo['source']) && $this->array_key_exists_recursive('source', $photo)) ) {
$facebook['photo'][$i]['description'] = $photo['name'];
$facebook['photo'][$i]['image_src'] = $photo['source'];
}
}
$i++;
}
}
I get a result similar to what I need, but with only 6 inner arrays (when there should be like 10 of them), showing the iteration isn't going correctly.
Please I'd appreciate to be pointed in the right direction.
Did this: array_push($facebook['photo'], array( 'description' => $photo['name'], 'image_src' => $photo['source'] ));
$facebook['photo'] = array();
foreach ($raw_facebook['albums']['data'] as $photos) {
if ($photos){
foreach($photos['photos']['data'] as $photo) {
if((isset($photo['name']) && array_key_exists('name', $photo)) && (isset($photo['source']) && $this->array_key_exists_recursive('source', $photo)) ){
array_push($facebook['photo'], array( 'description' => $photo['name'], 'image_src' => $photo['source'] ));
}
}
}
}
Here you go
$data = $raw_facebook['albums']['data']; //indexed array of objects
$output = array('photo' => array());
for($i = 0; $i< count($data); $i++){
$photosdataarray = $data[$i]['photos']['data'];
for($j = 0; $j < count($photosdataarray); $j++){
$output['photo'][] = array(
'description' => $photosdataarray[$j]['name'],
'image_src' => 'source',
);
}
}
Consider using collection pipeline for this kind of data processing. You'll end up with cleaner code (i.e. no foreach > if > foreach).
Example with your code (using Knapsack):
$photos = Collection::from($raw_facebook['albums']['data'])
->extract('photos.data')
->flatten(1)
->filter(function (array $data) {
return isset($data['name'])
&& isset($data['source'])
&& $this->array_key_exists_recursive('source', $data);
})
->map(function (array $data) {
return [
'description' => $data['name'],
'image_src' => $data['source']
];
})
->values()
->toArray();
Libraries:
https://dusankasan.github.io/Knapsack
https://laravel.com/docs/5.1/collections
https://github.com/cakephp/collection
Related
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
}
]
}
]
}
I have an object like this, and I'm working with "todos" array, only want update active status with specific indexs are given:
{
"_id": "61e7e78372d3221d2c5fb242",
"from_date": "2022/01/01 00:00:00",
"to_date": "2022/01/01 00:00:00",
"todos": [
{
"name": "sub1",
"desc": "desc1",
"active": true,
"owner": "61e6125db0f102060951aa53"
}, // index = 0
{
"name": "sub2",
"desc": "desc2",
"active": true,
"owner": "61e6125db0f102060951aa53"
}, // index = 1
...
]
}
The first I try update with: ['$set' => ['todos.0.active' => false]]
{
"0": {
"active": false
},
"name": "sub1",
"desc": "desc1",
"active": true,
"owner": "61e6125db0f102060951aa53"
},
active can be update to false, key 0 is generated. I continue try other:
array_push($pipeline, ['$set' => ['todos' => [
'$function' => [
'body' => 'function(todos) {
return todos; // or do something
}',
'args' => ['todos'],
'lang' => 'js',
]
]]]);
But todos field turn into string is "todos", not array. What wrong with $set and $function? And other operators can do the same easier like $map or $filter?
My full pipeline update:
[
{
"$set": {
"name": "First Task 1",
"desc": "This is description",
"status": "completed",
"done": 100,
"level": "medium",
"company": "BBB",
"project": "AAA",
"from_date": "2022/01/01 00:00:00",
"to_date": "2022/01/01 00:00:00",
"updated_at": "$$NOW"
}
},
{
"$set": {
"todos.1.active": false
}
},
{
"$set": {
"todos": {
"$concatArrays": [
"$todos",
[
{
"name": "sub3",
"desc": "desc3",
"active": true,
"owner": "61e6125db0f102060951aa53"
}
]
]
}
}
}
]
db.collection.updateOne(['_id' => ObjectId('')], $pipeline, ['multi' => true]})
I want to update with pipeline, not each single query.
db.collection.update({
"_id": "61e7e78372d3221d2c5fb242"
},
{
$set: {
"todos.$[elem].active": false
}
},
{
"multi": true,
"arrayFilters": [
{
"elem.name": {
$eq: "sub1"
}
}
]
})
mongoplayground
db.collection.update({
"_id": "61e7e78372d3221d2c5fb242"
},
{
$set: {
"todos.0.active": false,
"todos.2.active": false
}
},
{
"multi": true
})
mongoplayground
db.collection.update({
"_id": "61e7e78372d3221d2c5fb242"
},
[
{
$set: {
todos: {
$map: {
input: {
$range: [ 0, { $size: "$todos" } ]
},
as: "i",
in: {
$let: {
vars: {
"item": { $arrayElemAt: [ "$todos", "$$i" ] }
},
in: {
$cond: {
if: {
$in: [ "$$i", [ 0, 2 ] ]
},
then: {
"name": "$$item.name",
"desc": "$$item.desc",
"active": false,
"owner": "$$item.owner"
},
else: "$$item"
}
}
}
}
}
}
}
}
],
{
"multi": true
})
mongoplayground
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 into a project where I'm trying to get information from a json response (Again), that I got from google+.
I need to return an array after populating it with some information from the json response.
The [modified] json response is thus:
{
"items": [
{
"kind": "plus#activity",
"etag": "\"gLJf7Ldsgfsdgsgsf9ES9mEc/LFcp0tW7Ffgfsdgdf4Yfgdff3Z8\"",
"title": "first title",
"published": "2015-08-13T12:23:36.316Z",
"updated": "2015-08-13T12:23:36.316Z",
"id": "z12njfgogxirwdsoihosdhgjsdghusdn",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/8QZBMRjHVMF",
"actor": {
"id": "210973863758690009009",
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/AAAfgdagAAAI/AAAgdfgfdAPQ/oBXvfgdgdf1aeLQ/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": " \u003ca rel=\"nofollow\" class=\"ot-hashtag\" href=\"https://plus.google.com/s/%oko\"\u003es\u003c/a\u003e\ufeff",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/8QZiUyuyuobsvASKKHVMF",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/z12ZiUyuyuobsvASKKcxqnn/comments"
},
"plusoners": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/z12njfgogZiUyuyuobsvASKKcxqnn/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/z12njfZiUyuyuobsvASKKqnn/people/resharers"
},
"attachments": [
{
"objectType": "photo",
"displayName": "Photo 1",
"id": "210973863758690009009.6618960307328673257603073286732572860914",
"content": "123",
"url": "https://plus.google.com/photos/21096189603073286732572860914009/albums/616189603073286732572860914457/66189603073286732572860914385860914",
"image": {
"url": "https://lh5.googleusercontent.com/-2dgjFRZCII8/VcyMOOCHLTI/BAAHYGHDSJH/0XtjUD1perY/w506-h750/11017878_1655020691394612_1878283374465430482_n.jpg",
"type": "image/jpeg"
},
"fullImage": {
"url": "https://lh5.googleusercontent.com/-2dgjFRSDSDD8/VcyMSDSDSI/BAAHYGHDSJHDSIKA/0XtjIUYDIGIDGerY/11017878_1655020DSDKHSJ612_1878283374SDSDSD30482_n.jpg",
"type": "image/jpeg",
"height": 540,
"width": 540
}
}
]
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
},
{
"kind": "plus#activity",
"etag": "\"gLJf7LwN3wOpLHXk4IeQ9ES9mEc/TUMFP7Wc8QTGatBg1vGSEbjPPDE\"",
"title": "second title",
"published": "2015-08-13T11:57:49.262Z",
"updated": "2015-08-13T11:57:49.262Z",
"id": "z1iohyfsdugsjhockob0n232vjiz5xancxqnn",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/4DVBTreNkyA",
"actor": {
"id": "210973863758690009009",
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/BAAHYGHDSJH/BAAHYGHDSJH/oBXvDSDASAeLQ/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": "07012345678\ufeff",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/4DVBiodjjdmknaayA",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/pfjknsfubsnjbsb0n232vjiz5xancxqnn/comments"
},
"plusoners": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/pfjknsfubsnjbsb0n232vjiz5xancxqnn/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/pfjknsfubsnjbsb0n232vjiz5xancxqnn/people/resharers"
},
"attachments": [
{
"objectType": "photo",
"displayName": "Photo 2",
"id": "210973863758690009009.6837572367923789379",
"content": "456",
"url": "https://plus.google.com/photos/210973863758690009009/albums/6947862082892799114865/69843868408496745035842",
"image": {
"url": "https://lh3.googleusercontent.com/-Tvib82iMpsk/VcyGKh3UGAI/ADLJSKNSHJBSDNB AAQM/cyhYX3SifDY/w506-h750/1024-2006_1011_093752.jpg",
"type": "image/jpeg"
},
"fullImage": {
"url": "https://lh3.googleusercontent.com/-Tvijihjsdbnjgsdupsk/VcfjhjfsbjgsugAI/ApkdlnsdjkbjsdAQM/cyhYX3SifDY/w1024-h768/1024-2006_1011_093752.jpg",
"type": "image/jpeg",
"height": 768,
"width": 1024
}
}
]
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
},
{
"kind": "plus#activity",
"etag": "\"gLJfnjsdjnvdsvhsdIeQ9EpoifkjnfmEc/P4ihjsadiadakhlbhvdshvfXan4\"",
"title": "third title",
"published": "2015-08-12T22:57:22.010Z",
"updated": "2015-08-12T22:57:22.010Z",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/iiuhfdjhfidpnbvcW",
"actor": {
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/BAJDHJDSIDKAI/PODSIOUSDIKAAPQ/oBXvlp1aeLQ/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": " \u003ca rel=\"nofollow\" class=\"ot-hashtag\" href=\"https://plus.google.com/s/IUYDHJDSUGDSHGY\"\u003eIUYDHJDSUGDSHGY\u003c/a\u003e \ufeff",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/iukdjkdowqvcW",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/uihfjbsjkbsfdnfffdfdpokrouh3quig/comments"
},
"plusoners": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/uihfjbsjkbsfdnfffdfdpokrouh3quig/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/uihfjbsjkbsfdnfffdfdpokrouh3quig/people/resharers"
},
"attachments": [
{
"objectType": "photo",
"displayName": "Photo 3",
"content": "789",
"url": "https://plus.google.com/photos/210973863758690009009/albums/6182262671805531297/618654563857564534922",
"image": {
"url": "https://lh3.googleusercontent.com/-uihfjbsdhgsdilop/VcvPJmJ0bUI/wERRDFFAAAAN8/X6J9fHJVBFDVSFVSak/w506-h750/PicsArt_1438914428134.jpg",
"type": "image/jpeg"
},
"fullImage": {
"url": "https://lh3.googleusercontent.com/-uihfjbsdhgsdilop/VcvPJmJ0bUI/wERRDFFAAAAN8/XyghFGF2TgBak/w562-h562/PicsArt_1438914428134.jpg",
"type": "image/jpeg",
"height": 562,
"width": 562
}
}
]
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
},
{
"kind": "plus#activity",
"etag": "\"gLJioyuhgdugudsgugc/UGHAoodpuskhjhgsdjdbY0\"",
"title": "fourth title",
"published": "2011-09-26T17:13:14.309Z",
"updated": "2011-09-26T17:13:14.309Z",
"id": "z12odlnbgmavzdh4k04cgfhxvwrouh3quig",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/R9zieiuhdjghdgBFu",
"actor": {
"id": "210973863758690009009",
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/AKHDSJHSDGAAI/AUIDHIJGDSHGHAPQ/oBXvlp1aeLQ/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": "UIHUIGG FJGF GU",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/R9zStuw9BFu",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/oilzdh4k04cgpooiyuewfouh3quig/comments"
},
"plusoners": {
"totalItems": 1,
"selfLink": "https://www.googleapis.com/plus/v1/activities/oilzdh4k04cgpooiyuewfouh3quig/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/zkjhsdsfgiooi;oilzdh4k04cgpooiyuewfouh3quig/people/resharers"
}
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
},
{
"kind": "plus#activity",
"etag": "\"oilzdh4k04cgpooiyuewfouh3quig/oilzdh4k04cgpooiyuewfouh3quig\"",
"title": "fifth title",
"published": "2011-09-26T17:09:23.712Z",
"updated": "2011-09-26T17:09:23.712Z",
"id": "z13msxkxfnjjvp04p04cgfhxvwrouh3quig",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/RvXF5ufy23n",
"actor": {
"id": "210973863758690009009",
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/AAAAAAAAAAI/AAAAAAAAAPQ/oilzdh4k04cgpooiyuewfouh3quig/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": "witout 'ME' it's jst AWESO....\ufeff",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/RvXF5ufy23n",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/oilzdh4k04cgpooiyuewfouh3quig/comments"
},
"plusoners": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/oilzdh4k04cgpooiyuewfouh3quig/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/gLJfoiojkdwjkshdikhdhisEc/people/resharers"
}
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
},
{
"kind": "plus#activity",
"etag": "\"gLJfoiojkdwjkshdikhdhisEc/gLJfoiojkdwjkshdikhdhisEc\"",
"title": "sixth title",
"published": "2011-09-26T17:07:05.897Z",
"updated": "2011-09-26T17:07:05.897Z",
"id": "gLJfoiojkdwjkshdikhdhisEc",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/gLJfoiojkdwjkshdikhdhisEc",
"actor": {
"id": "210973863758690009009",
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/AASKJAKHJGASQAPQ/AASKJAKHJGASQAPQ/gLJfoiojkdwjkshdikhdhisEc/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": "dere's an xception 2 evry rule, xcept dis 1..\ufeff",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/N6QTZt2XvSR",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/gLJfoiojkdwjkshdikhdhisEc/comments"
},
"plusoners": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/gLJfoiojkdwjkshdikhdhisEc/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/gLJfoiojkdwjkshdikhdhisEc/people/resharers"
}
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
}
]
}
this is what I'm doing:
function google_pluss() {
$file_dir = strtolower(realpath(APPPATH . '..' . DIRECTORY_SEPARATOR . 'assets'));
$json_file = $file_dir . DIRECTORY_SEPARATOR . 'stackoverflow' . '.json'; // I have the file locally...
$json_data = json_decode(file_get_contents($json_file));
$data = array();
if (isset($json_data)) {
$x = 0;
foreach ($json_data->items as $item) {
if (isset($item->object->attachments)) {
foreach ($item->object->attachments as $attachment) {
$data['number'][$x] = $attachment->content;
$data['image'][$x] = $attachment->fullImage->url;
}
$data['title'][$x] = $item->title;
$x++;
}
}
}
print_r($data);
}
this is the output I get:
Array
(
[number] => Array
(
[0] => 123
[1] => 456
[2] => 789
)
[image] => Array
(
[0] => https://lh5.googleusercontent.com/-2dgjFRSDSDD8/VcyMSDSDSI/BAAHYGHDSJHDSIKA/0XtjIUYDIGIDGerY/11017878_1655020DSDKHSJ612_1878283374SDSDSD30482_n.jpg
[1] => https://lh3.googleusercontent.com/-Tvijihjsdbnjgsdupsk/VcfjhjfsbjgsugAI/ApkdlnsdjkbjsdAQM/cyhYX3SifDY/w1024-h768/1024-2006_1011_093752.jpg
[2] => https://lh3.googleusercontent.com/-uihfjbsdhgsdilop/VcvPJmJ0bUI/wERRDFFAAAAN8/XyghFGF2TgBak/w562-h562/PicsArt_1438914428134.jpg
)
[title] => Array
(
[0] => first title
[1] => second title
[2] => third title
)
)
But, this is the output I actually need:
Array
(
[0] => Array
(
[number] => 123
[image] => https://lh5.googleusercontent.com/-2dgjFRSDSDD8/VcyMSDSDSI/BAAHYGHDSJHDSIKA/0XtjIUYDIGIDGerY/11017878_1655020DSDKHSJ612_1878283374SDSDSD30482_n.jpg
[title] => first title
)
[1] => Array
(
[number] => 456
[image] => https://lh3.googleusercontent.com/-Tvijihjsdbnjgsdupsk/VcfjhjfsbjgsugAI/ApkdlnsdjkbjsdAQM/cyhYX3SifDY/w1024-h768/1024-2006_1011_093752.jpg
[title] => second title
)
[2] => Array
(
[number] => 789
[image] => https://lh3.googleusercontent.com/-uihfjbsdhgsdilop/VcvPJmJ0bUI/wERRDFFAAAAN8/XyghFGF2TgBak/w562-h562/PicsArt_1438914428134.jpg
[title] => third title
)
)
I'd really appreciate if someone shows me how to achieve this, thank you.
btw, I'm using the $x variable and incrementing it, to stop the loop where the array doesn't contain attachments. I'd be glad too, if shown how to do this neater.
At first glance, I would say change
foreach ($item->object->attachments as $attachment) {
$data['number'][$x] = $attachment->content;
$data['image'][$x] = $attachment->fullImage->url;
}
to
foreach ($item->object->attachments as $attachment) {
$data[$x]['number'] = $attachment->content;
$data[$x]['image'] = $attachment->fullImage->url;
}
My code:
foreach(array_filter($row) as $key => $value) {
$output[$value['phone']]['cards'][] = array(
'email' => $value['email'],
'mobile' => $value['mobile'],
'name' => $value['name']
);}
echo json_encode($output);
Current output:
{
"919898989898": {
"cards": [
{
"email": "vwxy#test.com",
"mobile": "919898989898",
"name": "abcd"
},
{
"email": "pnqr#gmail.com",
"mobile": "8686868686",
"name": "abcd"
}
]
},
"919923717198": {
"cards": [
{
"email": "abcd#gmail.com",
"mobile": "8686868686",
"name": "defg"
},
{
"email": "rstp#test.com",
"mobile": "919898989898",
"name": "defg"
}
]
}
}
Expected output:
{
"phone": [
{
"919923717198": {
"cards": [
{
"email": "abcd#gmail.com",
"mobile": "8686868686",
"name": "defg"
},
{
"email": "rstp#test.com",
"mobile": "919898989898",
"name": "defg"
}
]
}
}
]
}
Use this:
foreach(array_filter($row) as $key => $value) {
$data = [$value['phone']]['cards'][] = array(
'email' => $value['email'],
'mobile' => $value['mobile'],
'name' => $value['name']
);
$output['phone'][] = $data;
}
echo json_encode($output);