how to arrange multidimensional array - Group by value in php? - php

I want to arrange same date items to single index, I have following array -
Array(
[0] => Array
(
[date] => 30 Dec 2015
[record] => Array
(
[id] => 84675
[name] => Item1
)
)
[1] => Array
(
[date] => 28 Dec 2015
[record] => Array
(
[id] => 84675
[name] => item2
)
)
[2] => Array
(
[date] => 22 Nov 2015
[record] => Array
(
[id] => 2011
[name] => item3
)
)
[3] => Array
(
[date] => 22 Nov 2015
[record] => Array
(
[id] => 86649
[name] => item4
)
))
I want to arrange this array like -
Array(
[0] => Array
(
[date] => 30 Dec 2015
[record] => Array
(
[id] => 84675
[name] => Item1
)
)
[1] => Array
(
[date] => 28 Dec 2015
[record] => Array
(
[id] => 84675
[name] => item2
)
)
[2] => Array
(
[date] => 22 Nov 2015
[record] => Array
(
[id] => 2011
[name] => item3
),Array
(
[id] => 86649
[name] => item4
)
)
)
I want to arrange same date items into single index, can anybody please help me.I tried to arrange it using loops but couldn't get success.Any help would be really appreciated.
Thanks!

Try this code it can help
<?php
$result = array();
foreach($data as $info)
{
$result[$info['date']][] = $info;
}
print_r($result);
?>

You can do this in such a way also. In this way you will be able to get the dates in keys of $record_array and record values in values. Try this..
$your_array = array();
$record_array = array();
foreach ($your_array as $value){
$record_array[$value['date']][] = $value['record'];
}
print_r($record_array);
Hope this wil help

Related

Group array within group data php

After generating group array from an associative array, now the array is in the following format. And to make this group array I have used the following code:
foreach($upcoming_data_arr as $key => $item)
{
$arr[$item['year']][$key] = $item;
}
Array
(
[2018] => Array
(
[0] => Array
(
[id] => 6
[year] => 2018
[month] => 11
)
[1] => Array
(
[id] => 5
[year] => 2018
[month] => 12
)
[2] => Array
(
[id] => 4
[year] => 2018
[month] => 11
)
[3] => Array
(
[id] => 3
[year] => 2018
[month] => 11
)
)
)
Now I need another group array according to month within year group. And in this case group data array will be like following:
Array
(
[2018] => Array
(
[11] => Array
(
[0] => Array
(
[id] => 6
[year] => 2018
[month] => 11
)
[1] => Array
(
[id] => 4
[year] => 2018
[month] => 11
)
[2] => Array
(
[id] => 3
[year] => 2018
[month] => 11
)
)
[12] => Array
(
[0] => Array
(
[id] => 5
[year] => 2018
[month] => 12
)
)
)
)
How we will get this output?
Add one more "dimension" when you are creating an array identified by $item['month'] and let php decide last "dimension" key by []:
foreach($upcoming_data_arr as $key => $item) {
$arr[$item['year']][$item['month']][] = $item;
}

Add up multidimensional Sub Array Values by other Sub Array Values

I am working on a statistics Dashboard where I want to display some stats sorted by calendar weeks.
I'm getting the data in a multidimensional Array with this format:
Array
(
[success] => 1
[stats] => Array
(
[0] => Array
(
[year] => 2018
[week] => 14
[sum] => 18
[country] => at
)
[1] => Array
(
[year] => 2018
[week] => 14
[sum] => 907
[country] => de
)
[2] => Array
(
[year] => 2018
[week] => 15
[sum] => 2
[country] =>
)
[3] => Array
(
[year] => 2018
[week] => 15
[sum] => 65
[country] => at
)
[4] => Array
(
[year] => 2018
[week] => 15
[sum] => 237
[country] => de
)
)
)
My Problem is, that it's not only grouped by calendar week, but by country as well. What I want to get is an Array, which is grouped by calendar week. The sum should be the sum of every sum value with the same week.
The Array I need should look like this one:
Array
(
[success] => 1
[stats] => Array
(
[0] => Array
(
[year] => 2018
[week] => 14
[sum] => 925
)
[1] => Array
(
[year] => 2018
[week] => 15
[sum] => 304
)
)
)
The country is not needed anymore and it should be only 1 entry for each calendar week.
Something like this should do the trick
$newArray = [];
foreach ($firstArray['stats'] as $entry) {
if (isset($newArray[sprintf('%d-%d', $entry['year'], $entry['week'])])) {
$newArray[sprintf('%d-%d', $entry['year'], $entry['week'])]['sum'] += $entry['sum'];
} else {
$newArray[sprintf('%d-%d', $entry['year'], $entry['week'])] = $entry;
}
}
EDIT
$newArray = [];
foreach ($stats['stats'] as $entry) {
if (isset($newArray[sprintf('%d-%d', $entry['year'], $entry['week'])])) {
$newArray[sprintf('%d-%d', $entry['year'], $entry['week'])]['sum'] +=
$entry['sum'];
} else {
$newArray[sprintf('%d-%d', $entry['year'], $entry['week'])] = $entry;
}
unset($newArray[sprintf('%d-%d', $entry['year'], $entry['week'])]['country']);
}
$stats['stats'] = array_values($newArray);
#Rok D. Thanks for this one... I could adapt your example to perfectly fit my needs.

PHP Multidimensional Array Rearranging

Sir,
I am confused what should be exact title of this issues. Below is my problem
I have a multidimensional array like below
Array
(
[0] => Array
(
[0] => 2017-11-01
[1] => 9 Am
)
[1] => Array
(
[0] => 2017-11-02
[1] => 07 Pm
)
[2] => Array
(
[0] => 2017-11-03
[1] => 11 Pm
)
[3] => Array
(
[0] => 2017-11-04
[1] => 03 Pm
)
[4] => Array
(
[0] => 2017-11-01
[1] => 11 Am
)
[5] => Array
(
[0] => 2017-11-02
[1] => 05 Pm
)
)
Now, I want to make unique date beside time should be into another array.
I want it as below...
Array
(
[0] => Array
(
[0] => 2017-11-01
[1] => Array
(
[0] => 9 Am
[1] => 11 Am
)
)
[1] => Array
(
[0] => 2017-11-02
[1] => Array
(
[0] => 07 Pm
[1] => 05 Pm
)
)
[2] => Array
(
[0] => 2017-11-03
[1] => Array
(
[0] => 11 Pm
)
)
[3] => Array
(
[0] => 2017-11-04
[1] => Array
(
[0] => 03 Pm
)
)
)
Thanks in advance..
Regards,
Anwar
Something of that fashion should do the trick...
Its nowhere near the fastest way to do so but it should get you going and it is pretty easy to read/understand
// putting the times in a new array where the date is the key
$byDate = [];
foreach ($firstArray as [$date, $time]) {
$byDate[$date][] = $time;
}
// going thru it again to have it the way you needed it
$newArray = [];
foreach ($byDate as $date => $times) {
$newArray[] = [$date, $times];
}

return values from multidimensional array by the highest value of a specific value

I am trying to get the values in the [headers] array from the last array number. I then want to assign these values to variables to use in a foreach loop. The [headers] array numbers are sometimes different to other [headers] arrays, you can see this below.
The first [headers] array has 'From' and 'Date' under [0] => Array and [1] Array. The second [headers] array has 'From' and 'Date' under [11] => Array and [12] => Array.
So far I have only managed to get a first matched value using:
$arr = array($array);
$arrIt = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
foreach ($arrIt as $sub) {
$subArray = $arrIt->getSubIterator();
if ($subArray['name'] === 'From') {
$outputArray = iterator_to_array($subArray);
}
}
Using this array:
Array
(
[id] => 15acc078ff3c13bb
[historyId] => 30098
[messages] => Array
(
[0] => Array
(
[id] => 15acc078ff3c13bb
[threadId] => 15acc078ff3c13bb
[labelIds] => Array
(
[0] => Label_335
[1] => IMPORTANT
[2] => Label_332
[3] => CATEGORY_PERSONAL
[4] => INBOX
)
[snippet] => ok
[historyId] => 30084
[internalDate] => 1489481730000
[payload] => Array
(
[mimeType] => multipart/alternative
[filename] =>
[headers] => Array
(
[0] => Array
(
[name] => From
[value] => google#gmail.com
)
[1] => Array
(
[name] => Date
[value] => Tue, 14 Mar 2017 15:55:30 +0700
)
)
)
[1] => Array
(
[id] => 15acc09c623d48dd
[threadId] => 15acc078ff3c13bb
[labelIds] => Array
(
[0] => SENT
)
[snippet] => test On Tue, Mar 14, 2017 at 3:55 PM, test user wrote: > ok
[historyId] => 30098
[internalDate] => 1489481877000
[payload] => Array
(
[partId] =>
[mimeType] => text/plain
[filename] =>
[headers] => Array
(
[11] => Array
(
[name] => From
[value] => google2#gmail.com
)
[12] => Array
(
[name] => Date
[value] => Tue, 14 Mar 2017 15:57:57 +0700
)
)
)
)
)
)
You could do the following in order to iterate all headers with the structure of your array. Finding the last one should be trivial.
foreach($yourArray['messages'] as $messages){
foreach($messages['payload']['headers'] as $header){
// header will have those values:
/*
* [0] => Array
(
[name] => From
[value] => google#gmail.com
)
[1] => Array
(
[name] => Date
[value] => Tue, 14 Mar 2017 15:55:30 +0700
)
[0] => Array
(
[name] => From
[value] => google2#gmail.com
)
[1] => Array
(
[name] => Date
[value] => Tue, 14 Mar 2017 15:57:57 +0700
)
*/
}
}
you can use array_values to reindex the array.
$maxHistoryId = 0;
$headers = [];
foreach($array['id'] as $v)
{
if($v['historyId'] >= $max)
{
$max = $v['historyId'];
$headers = $v['payload']['headers'];
}
}
$resultArray = array_values($headers);
$resultArray = $resultArray[0];

How do I find max values in a multidimensional array?

I Have an array which looks like this:
Array
(
[0] => Array
(
[0] => Array
(
[product] => 2003
[date] => 2010-09-15 13:27:35
[status] => 3
)
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 6
)
)
[1] => Array
(
[0] => Array
(
[product] => 2003
[date] => 2010-09-12 13:27:35
[status] => 1
)
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 4
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
)
[2] => Array
(
[0] =>
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
)
I want to "collapse" each second dimension array and obtain the max DATE value and the max status value.So the first index would return 2010-09-18 13:27:35 and '6' etc.
The problem is further complicated by the empty array in the last index. I would like to use this empty array and report it as the MAX date and status.
Thank in advance!
Thanks for looking everybody. I figured it out.
$date=array();
$status=array();
$availability=array();
foreach($set as $key => $value)
{
foreach($value as $value2)
{
if(isset($value2[1]))
{
$date[$key][]=$value2[1];
$status[$key][]=$value2[2];
}
else
{
$date[$key][]='2022-09-18 13:27:35';
$status[$key][]='0';
}
}
$availability[$key]=array(max($date[$key]),min($status[$key]));
}

Categories