I'm stuck on this problem and I hope someone can help me on that.
I have an array which I want to group by a specific key. The only problem is that I want to have a new array whenever the value of the key changes during the loop. Here is an example of the array.
Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[2] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
[3] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[4] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[5] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[6] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
I did
foreach($report_items as $item)
{
$groupedItems[$item['grouped_by']][] = $item;
}
and I got
Array
(
[1] => Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[2] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[3] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[4] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
[4] => Array
(
[0] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[1] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
)
)
What I'm actually looking for is something like this, where the arrays are separated during the loop and a suffix is added to the key whenever the value changes.
Array
(
[1.1] => Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
[4.1] => Array
(
[0] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[1] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
)
[1.2] => Array
(
[0] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[2] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[3] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
)
I'm not really aware of a simple way to solve this, so I would appreciate any help. Thank you!
Not going to bother to recreate your full array here, I am using a reduced version with just the id and the grouped_by value, but the principle is of course exactly the same if you do it with your "full" array.
I am using a counter array to keep track of which "iteration" of a specific group we are currently dealing with. In classic control break implementation logic, I am comparing the grouped_by of the current record, with that of the previous one - if those differ, then the counter for this grouped_by value has to be incremented by 1. And then, I am simply creating the array key to use, by combining the grouped_by value, and the current count for it.
$data = [['id' => 972, 'grouped_by' => 1], ['id' => 664, 'grouped_by' => 4], ['id' => 631, 'grouped_by' => 4], ['id' => 630, 'grouped_by' => 1], ['id' => 971, 'grouped_by' => 1], ['id' => 973, 'grouped_by' => 1], ['id' => 974, 'grouped_by' => 1]];
$grouped_result = $grouped_by_counts = [];
$previous_grouped_by = null; // a value that will never occur in the data
foreach($data as $datum) {
if($datum['grouped_by'] !== $previous_grouped_by) {
$grouped_by_counts[$datum['grouped_by']] =
isset($grouped_by_counts[$datum['grouped_by']]) ?
$grouped_by_counts[$datum['grouped_by']] + 1 : 1;
}
$grouped_result[
$datum['grouped_by'].'.'.$grouped_by_counts[$datum['grouped_by']]
][] = $datum;
$previous_grouped_by = $datum['grouped_by'];
}
print_r($grouped_result);
Live example: https://3v4l.org/odtie
Related
I have an array of arrays that I need to consolidate into another array.
I have tried mapping over it, matching object_id, and gathering all account_ids for said object_id, but all my attempts are not even close as I am trying to learn PHP
This is the original array
[0] => Array
(
[rank] => 0
[id] => 6
[object_id] => 3
[account_id] => 13
)
[1] => Array
(
[rank] => 1
[id] => 7
[object_id] => 3
[account_id] => 565
)
[2] => Array
(
[rank] => 2
[id] => 1823
[object_id] => 825
[account_id] => 563
)
[3] => Array
(
[rank] => 3
[id] => 1824
[object_id] => 825
[account_id] => 564
)
[4] => Array
(
[rank] => 4
[id] => 1825
[object_id] => 825
[account_id] => 565
)
[5] => Array
(
[rank] => 5
[id] => 7187
[object_id] => 3113
[account_id] => 564
)
[6] => Array
(
[rank] => 6
[id] => 7188
[object_id] => 3113
[account_id] => 565
)
This is the desired result
[3] => [13, 565],
[825] => [563, 564, 565],
[3113] => [564, 565],
You need to create a new array by using object_id index.
Example:
<?
$array = array(
array('rank'=>0,'id'=>6,'object_id'=>3,'account_id'=>13),
array('rank'=>1,'id'=>7,'object_id'=>3,'account_id'=>565),
array('rank'=>2,'id'=>1823,'object_id'=>825,'account_id'=>563),
array('rank'=>3,'id'=>1824,'object_id'=>825,'account_id'=>564),
array('rank'=>4,'id'=>1825,'object_id'=>825,'account_id'=>565),
array('rank'=>5,'id'=>7187,'object_id'=>3113,'account_id'=>564),
array('rank'=>6,'id'=>7188,'object_id'=>3113,'account_id'=>565),
);
$newArray = array(); // initiliaze array
foreach ($array as $key => $value) {
$newArray[$value['object_id']][] = $value['account_id']; // save it in group
}
echo "<pre>";
print_r($newArray); // result
?>
Running Example
I have array of categories like this and I want to merge the same value like 1 and 0 index value etc in another new array how it is possible.
Array
(
[0] => Array
(
[id] => 29
[parentId] => 0
[serviceName] => Hair
[parentServiceImg] => 7ffb7f5aa2a210ba927e9de64ef17f93.jpeg
[subCategory] => Array
(
[id] => 30
[parentId] => 29
[serviceName] => Blow Out
[subCategory] => Array
(
[id] => 31
[parentId] => 30
[serviceName] => Add Clients Hair Extensions & Style
[subCategory] => Array
(
[id] => 49
[parentId] => 31
[serviceName] => Hair style color
[servicePrice] => 100
)
)
)
)
[1] => Array
(
[id] => 1
[parentId] => 0
[serviceName] => Makeup
[parentServiceImg] => e00d3576847fef9f717d1de2647ed954.jpeg
[subCategory] => Array
(
[id] => 2
[parentId] => 1
[serviceName] => Special Event Makeup
[subCategory] => Array
(
[id] => 3
[parentId] => 2
[serviceName] => Add Airbrush
[servicePrice] => 232
)
)
)
[2] => Array
(
[id] => 1
[parentId] => 0
[serviceName] => Makeup
[parentServiceImg] => e00d3576847fef9f717d1de2647ed954.jpeg
[subCategory] => Array
(
[id] => 6
[parentId] => 1
[serviceName] => Photoshot Makeup
[subCategory] => Array
(
[id] => 7
[parentId] => 6
[serviceName] => Add Airbrush
[servicePrice] => 8
)
)
)
[3] => Array
(
[id] => 1
[parentId] => 0
[serviceName] => Makeup
[parentServiceImg] => e00d3576847fef9f717d1de2647ed954.jpeg
[subCategory] => Array
(
[id] => 2
[parentId] => 1
[serviceName] => Special Event Makeup
[subCategory] => Array
(
[id] => 4
[parentId] => 2
[serviceName] => Add Lashes
[servicePrice] => 6
)
)
)
[4] => Array
(
[id] => 29
[parentId] => 0
[serviceName] => Hair
[parentServiceImg] => 7ffb7f5aa2a210ba927e9de64ef17f93.jpeg
[subCategory] => Array
(
[id] => 2
[parentId] => 1
[serviceName] => Special Event Makeup
[subCategory] => Array
(
[id] => 47
[parentId] => 29
[serviceName] => Wedding Day Mother of the Bride / Mother of the Groom Hair Touch Ups
[servicePrice] => 5
)
)
)
)
I want to 0 index value merge with 4 index value and get on 0 index of new array and 1 to 3 index value merge and get on 1 index in new array using php. I need help for this. thanks.
It should help you:
<?php
$your_array = [0=>['array1'], 1=>['array2'],2=>['array3'], 3=>['array4'], 4=>['array5']];
$new_array = [];
$first = array_shift($your_array);
$last = array_pop($your_array);
$array = array_merge($first, $last);
$new_array[] = $array;
$new_array[] = $your_array;
?>
Below is My Array. I want to make all possible combinations of array whose sum is equal to some number(e.g 20). The super parent array is continent and it's child array is it's countries and countries child array is its respective cities. i want to get all combinations of countries whose cities sum of days_range equal to some number(e.g 20). This is just a single continent array. it may be possible that the array is for other continents are also available.
Array
(
[1] => Array
(
[10] => Array
(
[0] => Array
(
[id] => 18
[city_name] => Baku
[country_id] => 10
[country_name] => Azerbaijan
[days_range] => 5
[continent_id] => 1
)
[2] => Array
(
[id] => 43
[city_name] => Lahıc
[country_id] => 10
[country_name] => Azerbaijan
[days_range] => 5
[continent_id] => 1
)
)
[23] => Array
(
[0] => Array
(
[id] => 42
[city_name] => Vientiane
[country_id] => 23
[country_name] => Laos
[days_range] => 7
[continent_id] => 1
)
[1] => Array
(
[id] => 47
[city_name] => Vang Vieng
[country_id] => 23
[country_name] => Laos
[days_range] => 3
[continent_id] => 1
)
)
[20] => Array
(
[0] => Array
(
[id] => 37
[city_name] => Tamsui District
[country_id] => 20
[country_name] => Taiwan
[days_range] => 1
[continent_id] => 1
)
[1] => Array
(
[id] => 35
[city_name] => Taipei
[country_id] => 20
[country_name] => Taiwan
[days_range] => 3
[continent_id] => 1
)
)
[22] => Array
(
[2] => Array
(
[id] => 46
[city_name] => Maolin District
[country_id] => 20
[country_name] => Taiwan
[days_range] => 3
[continent_id] => 1
)
[3] => Array
(
[id] => 36
[city_name] => Tainan
[country_id] => 20
[country_name] => Taiwan
[days_range] => 3
[continent_id] => 1
)
)
)
)
Below is my expected Output.
Array(
[0]=Array(
[0]=>[10],
[1]=>[23],
)
[1]=Array(
[0]=>[10],
[1]=>[20],
[2]=>[22],
)
[2]=Array(
[0]=>[23],
[1]=>[20],
[2]=>[22],
)
)
Hi my zend paginator works well when the $business_list array format is like below.
but when the array format changes it displays only one page instead of many pages.
$paginator = Zend_Paginator::factory($business_list);
Array
(
[0] => Array
(
[id] => 216
[userid] => 141
[title] => first req
[image] =>
[logo] =>
[description] =>
this is the first requirment
[date] => 2012-06-12 10:31:01
[area] => 1
[budget] => 1
[type] => 1
[status] => 1
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 315
[business_id] => 216
[section_id] => 1
[subsection_id] => 13
[description] =>
)
[1] => Array
(
[id] => 316
[business_id] => 216
[section_id] => 3
[subsection_id] => 14
[description] =>
)
[2] => Array
(
[id] => 317
[business_id] => 216
[section_id] => 4
[subsection_id] => 15
[description] =>
)
)
[CmUser] => Array
(
[id] => 141
[username] => venki
[password] =>
[firstname] => venkatesh
[lastname] => abus
[image] => 54winter.jpg
[email] => xxx#xx.com
[phone] => 23423452
[group_id] => 2
[status] =>
[registered_date] => 2012-06-04 06:32:58
[last_visit] => 0000-00-00 00:00:00
[is_active] => 1
[subscribe] => 1
)
)
[1] => Array
(
[id] => 214
[userid] => 98
[title] => gopicontractor
[image] => 54bluehills.jpg
[logo] => 239waterlilies.jpg
[description] =>
TIt uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is
[date] => 2012-06-11 12:18:58
[area] => 1
[budget] => 1
[type] => 3
[status] => 1
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 305
[business_id] => 214
[section_id] => 1
[subsection_id] => 5
[description] =>
)
[1] => Array
(
[id] => 306
[business_id] => 214
[section_id] => 1
[subsection_id] => 6
[description] =>
)
[2] => Array
(
[id] => 307
[business_id] => 214
[section_id] => 3
[subsection_id] => 1
[description] =>
)
[3] => Array
(
[id] => 308
[business_id] => 214
[section_id] => 4
[subsection_id] => 9
[description] =>
)
)
[CmUser] => Array
(
[id] => 98
[username] => gopi.s
[password] =>
[firstname] => venkatesh
[lastname] => franc
[image] =>
[email] => ss#ss.com
[phone] => 23423452
[group_id] => 3
[status] =>
[registered_date] => 2012-05-16 12:36:57
[last_visit] => 0000-00-00 00:00:00
[is_active] => 1
[subscribe] => 1
)
)
)
the above array format displays paginatin correctly.
This is the array that displays only one page in pagination.
Zend_Paginator Object
(
[_cacheEnabled:protected] => 1
[_adapter:protected] => Zend_Paginator_Adapter_Array Object
(
[_array:protected] => Array
(
[0] => Array
(
[id] => 89
[username] => xx
[password] => xx
[firstname] => xx
[lastname] => xx
[image] =>
[email] => xx#ymail.com
[phone] => 2342345
[group_id] => 2
[status] => offline
[registered_date] => 2012-05-15 10:58:53
[last_visit] =>
[is_active] => 1
[subscribe] => 0
[CmBusiness] => Array
(
[0] => Array
(
[id] => 204
[userid] => 89
[title] => xxhousing
[image] => 760067.jpg
[logo] => xx_818f3c97e6_o.jpg
[description] => xx
[date] => 2012-05-31 13:36:17
[area] => 1
[budget] => 1
[type] => 1
[status] => 1
[CmAmount] => Array
(
[id] => 1
[amount] => 1000-1500
[status] => 1
)
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 251
[business_id] => 204
[section_id] => 1
[subsection_id] => 6
[description] => xx
)
[1] => Array
(
[id] => 252
[business_id] => 204
[section_id] => 3
[subsection_id] => 2
[description] => xx
)
[2] => Array
(
[id] => 253
[business_id] => 204
[section_id] => 3
[subsection_id] => 4
[description] => xx
)
[3] => Array
(
[id] => 254
[business_id] => 204
[section_id] => 4
[subsection_id] => 9
[description] => xx
)
)
[CmArea] => Array
(
[id] => 1
[area] => manchester
[status] => 1
)
[CmType] => Array
(
[id] => 1
[type] => Personal business
[status] => 1
)
)
[1] => Array
(
[id] => 205
[userid] => 89
[title] => xx
[image] => 41217850-desktop-wallpapers-new-windows-xp.jpg
[logo] => 356anger_n.jpg
[description] => xx
[date] => 2012-05-31 13:37:15
[area] => 1
[budget] => 1
[type] => 3
[status] => 1
[CmAmount] => Array
(
[id] => 1
[amount] => 1000-1500
[status] => 1
)
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 255
[business_id] => 205
[section_id] => 1
[subsection_id] => 6
[description] =>xx
)
[1] => Array
(
[id] => 256
[business_id] => 205
[section_id] => 3
[subsection_id] => 1
[description] => xx
)
[2] => Array
(
[id] => 257
[business_id] => 205
[section_id] => 3
[subsection_id] => 2
[description] => xx
)
[3] => Array
(
[id] => 258
[business_id] => 205
[section_id] => 4
[subsection_id] => 10
[description] => xx
)
)
[CmArea] => Array
(
[id] => 1
[area] => manchester
[status] => 1
)
[CmType] => Array
(
[id] => 3
[type] => Bilde companies
[status] => 1
)
)
[2] => Array
(
[id] => 206
[userid] => 89
[title] => Nuestros recursos
[image] =>
[logo] =>
[description] => xx
[date] => 2012-05-31 13:38:04
[area] => 1
[budget] => 1
[type] => 4
[status] => 1
[CmAmount] => Array
(
[id] => 1
[amount] => 1000-1500
[status] => 1
)
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 259
[business_id] => 206
[section_id] => 1
[subsection_id] => 5
[description] =>
)
[1] => Array
(
[id] => 260
[business_id] => 206
[section_id] => 1
[subsection_id] => 6
[description] =>
)
[2] => Array
(
[id] => 261
[business_id] => 206
[section_id] => 3
[subsection_id] => 2
[description] =>
)
[3] => Array
(
[id] => 262
[business_id] => 206
[section_id] => 4
[subsection_id] => 10
[description] =>
)
)
[CmArea] => Array
(
[id] => 1
[area] => manchester
[status] => 1
)
[CmType] => Array
(
[id] => 4
[type] => Designer
[status] => 1
)
)
)
)
)
[_count:protected] => 1
)
[_currentItemCount:protected] =>
[_currentItems:protected] =>
[_currentPageNumber:protected] => 1
[_filter:protected] =>
[_itemCountPerPage:protected] => 2
[_pageCount:protected] => 1
[_pageRange:protected] =>
[_pages:protected] =>
[_view:protected] =>
)
How can I sort the below array by any of the child values (i.e: post_id, username, etc...)?
Array(
[0] => Array
(
[id] => 10
[post_id] => 398
[user_id] => 0
[view_date] => 2010-11-20 15:26:00
[views] => 4
[username] => <em>guest</em>
)
[1] => Array
(
[id] => 9
[post_id] => 397
[user_id] => 1
[view_date] => 2010-11-19 23:35:39
[views] => 1
[username] => Fire G
)
[2] => Array
(
[id] => 8
[post_id] => 398
[user_id] => 1
[view_date] => 2010-11-19 23:35:26
[views] => 4
[username] => Fire G
)
[3] => Array
(
[id] => 7
[post_id] => 396
[user_id] => 1
[view_date] => 2010-11-19 23:07:57
[views] => 3
[username] => Fire G
)
[4] => Array
(
[id] => 6
[post_id] => 396
[user_id] => 1
[view_date] => 2010-11-19 23:07:55
[views] => 3
[username] => Fire G
)
[5] => Array
(
[id] => 5
[post_id] => 396
[user_id] => 1
[view_date] => 2010-11-19 23:07:52
[views] => 3
[username] => Fire G
)
[6] => Array
(
[id] => 4
[post_id] => 13
[user_id] => 1
[view_date] => 2010-11-19 22:59:35
[views] => 1
[username] => Fire G
)
[7] => Array
(
[id] => 3
[post_id] => 3
[user_id] => 1
[view_date] => 2010-11-19 22:59:28
[views] => 1
[username] => Fire G
)
[8] => Array
(
[id] => 2
[post_id] => 398
[user_id] => 1
[view_date] => 2010-11-18 11:37:20
[views] => 4
[username] => Fire G
)
[9] => Array
(
[id] => 1
[post_id] => 398
[user_id] => 1
[view_date] => 2010-11-18 11:37:16
[views] => 4
[username] => Fire G
)
)
Use uasort, user-defined sorting that lets you provide your own function to define whether one element should come before another.
http://www.php.net/manual/en/function.uasort.php
Your comparator function would be something like
function cmp($a, $b) {
return $a['post_id'] > $b['post_id'] ? 1 : -1;
}