How to sum (sub total) array based on two keys? - php

I have array with below values
Array
(
[0] => Array
(
[order_date] => 2016-01-01
[sku] => RK101
[qty] => 2
)
[1] => Array
(
[order_date] => 2016-01-01
[sku] => RK101
[qty] => 5
)
[2] => Array
(
[order_date] => 2016-01-01
[sku] => RK102
[qty] => 4
)
[3] => Array
(
[order_date] => 2016-01-02
[sku] => RK101
[qty] => 2
)
[4] => Array
(
[order_date] => 2016-01-02
[sku] => RK101
[qty] => 2
)
)
and i want to sum the array, first by date and then by sku, i want result like below
Array
(
[2016-01-01] => Array
(
[RK101] => Array
(
[qty] => 7
)
[RK102] => Array
(
[qty] => 4
)
)
[2016-01-01] => Array
(
[RK101] => Array
(
[qty] => 4
)
)
)
i have explore stack overflow and found many post that calculates the sum based on one key, by in this case i want to sum the array values based on date first and then by sku.
please help with the same

try below solution:
<?php
echo '<pre>';
$array = Array
(
'0' => Array
(
'order_date' => '2016-01-01',
'sku' => 'RK101',
'qty' => 2
),
'1' => Array
(
'order_date' => '2016-01-01',
'sku' => 'RK101',
'qty' => 5
),
'2' => Array
(
'order_date' => '2016-01-01',
'sku' => 'RK102',
'qty' => 4
),
'3' => Array
(
'order_date' => '2016-01-02',
'sku' => 'RK101',
'qty' => 2
),
'4' => Array
(
'order_date' => '2016-01-02',
'sku' => 'RK101',
'qty' => 2
)
);
$new_array = array();
foreach ($array as $a) {
if (!isset($new_array[$a['order_date']][$a['sku']]['qty'])) {
$new_array[$a['order_date']][$a['sku']]['qty'] = 0;
}
$new_array[$a['order_date']][$a['sku']]['qty'] += $a['qty'];
}
print_r($new_array);
output:
Array
(
[2016-01-01] => Array
(
[RK101] => Array
(
[qty] => 7
)
[RK102] => Array
(
[qty] => 4
)
)
[2016-01-02] => Array
(
[RK101] => Array
(
[qty] => 4
)
)
)

Here is another solution that gives you a slightly different output, which may or may not be preferred.
$results = array();
$myNums = array(
array(
'order_date' => '2016-01-01',
'sku' => 'RK101',
'qty' => 2,
),
array(
'order_date' => '2016-01-01',
'sku' => 'RK101',
'qty' => 1,
),
array(
'order_date' => '2016-01-02',
'sku' => 'RK101',
'qty' => 2,
),
);
foreach ($myNums as $num) {
$key = "{$num['order_date']}|{$num['sku']}";
if (! array_key_exists($key, $results)) {
$results[$key] = 0;
}
$results[$key] += $num['qty'];
}
var_dump($results);
/*
var_dump gives the following...
Array
(
[2016-01-01|RK101] => 3,
[2016-01-02|RK101] => 2,
)*/

Related

Combine two arrays and keep the array name as the key in the newly combined array

I have two arrays, $array1 and $array2. When combining the two arrays, the expected output is to combine both arrays based on their keys, and keep the array name as the key. I'm using array_merge_recursive($array1,$array2); which combines the two arrays, but keeping the array name as the key does not work with this.
$array1 = array(
'mobile' => array(
array('item' => 'apple','price' => 4),
array('item' => 'nokia','price' => 39),
array('item' => 'samsung','price' => 8)
),
'tv' => array(
array('item' => 'LG','price' => 39),
array( 'item' => 'max', 'price' => 8 ),
array('item' => 'diaken','price' => 3 )
)
) ;
$array2 = array(
'mobile' => array(
array('item' => 'HTC','price' => 4 ),
array('item' => 'OnePlus' ,'price' => 39),
array ('item' => 'Nexus','price' => 8 )
),
'tv' => array(
array('item' => 'LG','price' => 39),
array('item' => 'Panasonic','price' => 8 ),
array('item' => 'Toshiba' ,'price' => 3 )
)
);
The output should be:
array(
'mobile' => array(
'array1' => array(
'0' => array('item' => 'apple','price' => 4),
'1' => array('item' => 'nokia','price' => 39),
'2' => array('item' => 'samsung','price' => 8),
)
'array2' => array (
'0' => array('item' => 'HTC','price' => 4),
'1' => array('item' => 'OnePlus','price' => 39),
'2' => array('item' => 'Nexus','price' => 8)
)
),
'tv' => array(
'array1' => array(
'0' => array('item' => 'LG','price' => 39),
'1' => array('item' => 'max','price' => 8),
'2' => array('item' => 'diaken','price' => 3)
),
'array2' => array(
'0' => array('item' => 'LG','price' => 39),
'1' => array('item' => 'max','price' => 8),
'2' => array('item' => 'diaken','price' => 3)
)
)
)
I used array_merge_recursive($array1,$array2);, and instead of getting the above, I get the following:
Array
(
'mobile' => Array
(
Array('item' => 'apple','price' => 4),
Array('item' => 'nokia','price' => 39),
Array('item' => 'samsung','price' => 8),
Array('item' => 'HTC','price' => 4 ),
Array('item' => 'OnePlus' ,'price' => 39),
Array ('item' => 'Nexus','price' => 8 ),
)
'tv' => Array
(
Array('item' => 'LG','price' => 39),
Array( 'item' => 'max', 'price' => 8 ),
Array('item' => 'diaken','price' => 3 ),
Array ('item' => 'LG','price' => 39),
Array('item' => 'Panasonic','price' => 8 ),
Array('item' => 'Toshiba' ,'price' => 3 )
)
)
Whilst I don't know how you're going to automate the "discovery" of your array soruces, it should be as simple as this:
$array = array();
$array['mobile'] = array();
$array['mobile'][] = $array1['mobile'];
$array['mobile'][] = $array2['mobile'];
$array['tv'] = array();
$array['tv'][] = $array1['tv'];
$array['tv'][] = $array2['tv'];
// Using the array keys as the worker
$array = array();
foreach( array_keys( array_merge( $array1, $array2 ) ) as $key )
{
$array[$key] = array();
$array[$key][] = $array1[$key];
$array[$key][] = $array2[$key];
}
first array
$array1 = array
(
'mobile' => array
(
'0' => array
(
'item' => 'apple',
'price' => 4
),
'1' => array
(
'item' => 'nokia',
'price' => 39
),
'2' => array
(
'item' => 'samsung',
'price' => 8
),
),
'tv' => array
(
'0' => array
(
'item' => 'LG',
'price' => 39
),
'1' => array
(
'item' => 'max',
'price' => 8
),
'2' => array
(
'item' => 'diaken',
'price' => 3
),
)
) ;
second array
$array2 = array
(
'mobile' => array
(
'0' => array
(
'item' => 'HTC',
'price' => 4
),
'1' => array
(
'item' => 'OnePlus',
'price' => 39
),
'2' => array
(
'item' => 'Nexus',
'price' => 8
),
),
'tv' => array
(
'0' => array
(
'item' => 'LG',
'price' => 39
),
'1' => array
(
'item' => 'Panasonic',
'price' => 8
),
'2' => array
(
'item' => 'Toshiba',
'price' => 3
),
)
);
marge two array and print result
$array['mobile']['array1'] = $array1['mobile'];
$array['mobile']['array2'] = $array2['mobile'];
$array['tv']['array1'] = $array1['tv'];
$array['tv']['array2'] = $array2['tv'];
echo '<pre>';
print_r($array);
echo '</pre>';
output
Array
(
[mobile] => Array
(
[array1] => Array
(
[0] => Array
(
[item] => apple
[price] => 4
)
[1] => Array
(
[item] => nokia
[price] => 39
)
[2] => Array
(
[item] => samsung
[price] => 8
)
)
[array2] => Array
(
[0] => Array
(
[item] => HTC
[price] => 4
)
[1] => Array
(
[item] => OnePlus
[price] => 39
)
[2] => Array
(
[item] => Nexus
[price] => 8
)
)
)
[tv] => Array
(
[array1] => Array
(
[0] => Array
(
[item] => LG
[price] => 39
)
[1] => Array
(
[item] => max
[price] => 8
)
[2] => Array
(
[item] => diaken
[price] => 3
)
)
[array2] => Array
(
[0] => Array
(
[item] => LG
[price] => 39
)
[1] => Array
(
[item] => Panasonic
[price] => 8
)
[2] => Array
(
[item] => Toshiba
[price] => 3
)
)
)
)
i would like to do simply do following --
// loop through first array
$tempArray = [];
foreach($array1 as $key=>$object)
{
$tempArray[$key] = [];
array_push($tempArray[$key], $object);
array_push($tempArray[$key],$array2[$key]);
}
echo '<pre>';
print_r($tempArray);
echo '</pre>';
this will give you exact output what you want. Irrespective of how many keys exists in the array. You need to determine on which array loop should be imposed.
Please check below code if array1 and array2 are fixed as per your above comment
$array_keys = array_keys( array_merge( $array1, $array2));
$final_array = array();
foreach($array_keys as $akey)
{
if(array_key_exists($akey, $array1))
$final_array[$akey]['array1'] = $array1[$akey];
if(array_key_exists($akey, $array2))
$final_array[$akey]['array2'] = $array2[$akey];
}
echo "<pre>";
print_r($final_array);
echo "</pre>";
Input code:
$array1 = array(
'mobile' => array(
array('item' => 'apple','price' => 4),
array('item' => 'nokia','price' => 39),
array('item' => 'samsung','price' => 8)
),
'tv' => array(
array('item' => 'LG','price' => 39),
array( 'item' => 'max', 'price' => 8 ),
array('item' => 'diaken','price' => 3 )
)
) ;
$array2 = array (
'mobile' => array (
0 => array ('item' =>'HTC','price' => 4,),
1 => array ('item' =>'OnePlus','price' => 39,),
2 => array ('item' =>'Nexus','price' => 8,)
),
'tv' => array (
0 => array ('item' =>'LG','price' => 39,),
1 => array ('item' =>'Panasonic','price' => 8,),
2 => array ('item' =>'Toshiba','price' => 3)
)
);
o/p:
Array
(
[mobile] => Array
(
[array1] => Array
(
[0] => Array
(
[item] => apple
[price] => 4
)
[1] => Array
(
[item] => nokia
[price] => 39
)
[2] => Array
(
[item] => samsung
[price] => 8
)
)
[array2] => Array
(
[0] => Array
(
[item] => HTC
[price] => 4
)
[1] => Array
(
[item] => OnePlus
[price] => 39
)
[2] => Array
(
[item] => Nexus
[price] => 8
)
)
)
[tv] => Array
(
[array1] => Array
(
[0] => Array
(
[item] => LG
[price] => 39
)
[1] => Array
(
[item] => max
[price] => 8
)
[2] => Array
(
[item] => diaken
[price] => 3
)
)
[array2] => Array
(
[0] => Array
(
[item] => LG
[price] => 39
)
[1] => Array
(
[item] => Panasonic
[price] => 8
)
[2] => Array
(
[item] => Toshiba
[price] => 3
)
)
)
)

Organizing multi-dimensional array of inventory

I need help organizing my inventory array. The structure is one big inventory array, with array of items inside. Per item array consists of the following:
item, item_group, item_no and array sold. sold consists of inner arrays with dates and quantity. Now, I'm having trouble organizing it for my needed output. I'll give you guys sample of input and output. So please do check and it's very much appreciated.
Sample part of my $inventory array
Array
(
[0] => Array
(
[item] => NK
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 11
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 1
[date] => 2017-10-29
)
)
[item_no] => 1
)
[1] => Array
(
[item] => FL
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 7
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 2
[date] => 2017-10-29
)
)
[item_no] => 2
)
[2] => Array
(
[item] => AD
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 5
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 3
[date] => 2017-10-29
)
)
[item_no] => 3
)
[3] => Array
(
[item] => CV
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 4
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 6
[date] => 2017-10-29
)
)
[item_no] => 4
)
[4] => Array
(
[item] => NB
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 12
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 4
[date] => 2017-10-29
)
)
[item_no] => 5
)
[5] => Array
(
[item] => SP
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 4
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 6
[date] => 2017-10-29
)
)
[item_no] => 6
)
[6] => Array
(
[item] => WB
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 5
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 2
[date] => 2017-10-29
)
)
[item_no] => 7
)
[7] => Array
(
[item] => wny
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 4
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 6
[date] => 2017-10-29
)
)
[item_no] => 8
)
[8] => Array
(
[item] => bs
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 15
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 2
[date] => 2017-10-29
)
)
[item_no] => 9
)
[9] => Array
(
[item] => st
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 16
[date] => 2017-10-29
)
)
[item_no] => 10
)
[10] => Array
(
[item] => ayhtdws
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 2
[date] => 2017-10-29
)
)
[item_no] => 11
)
[11] => Array
(
[item] => sif
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 3
[date] => 2017-10-29
)
)
[item_no] => 12
)
[12] => Array
(
[item] => bb
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 6
[date] => 2017-10-29
)
)
[item_no] => 13
)
)
From there, what I want to display is like this. Grouped by date ascending. And each item => quantity sold
Array
(
[0] => Array
(
[date] => 2017-10-28
[NK] => 11
[FL] => 7
[AD] => 5
[CV] => 4
[NB] => 12
[SP] => 4
[WB] => 5
[wny] => 4
[bs] => 15
)
[1] => Array
(
[date] => 2017-10-29
[NK] => 1
[FL] => 2
[AD] => 3
[CV] => 6
[NB] => 4
[SP] => 6
[WB] => 2
[wny] => 6
[bs] => 2
[st] => 16
[ayhtdws] => 2
[sif] => 3
[bb] => 6
)
)
I've spent almost 3 days figuring this out and up to this writing, I was only able to make it this far
$result = array();
$dates = array();
foreach ($inventory as $key => $item) {
foreach ($item['sold'] as $k => $v) {
array_push($dates, $v['date']);
}
}
$dates = array_unique($dates);
foreach($dates as $key => $value) {
array_push($result, array('date' => $value));
}
foreach ($dates as $rkey => $rvalue) {
foreach ($inventory as $key => $item) {
foreach ($item['sold'] as $k => $v) {
if ($v['date'] = $result[$key]['date']) {
array_push($result[$key][$item['item']] = $v['quantity']);
}
}
}
}
return $result;
Which of course gives me this sad result
Array
(
[0] => Array
(
[date] => 2017-10-28
[NK] => 1
)
[1] => Array
(
[date] => 2017-10-29
[FL] => 2
)
)
And to make things worse, we have this rule about cyclomatic complexities that we should only have at most 3 loop/conditions and up to 3 nesting levels per loop/conditions. And the whole organizing should not have any user created functions.
Even if not following the rules, I wasn't able to figure it out for days. Sorry if problem is long. Please help :(
Update: var_export($inventory) output
array (
0 =>
array (
'item' => 'NK',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '11',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '1',
'date' => '2017-10-29',
),
),
'item_no' => '1',
),
1 =>
array (
'item' => 'FL',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '7',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '2',
'date' => '2017-10-29',
),
),
'item_no' => '2',
),
2 =>
array (
'item' => 'AD',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '5',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '3',
'date' => '2017-10-29',
),
),
'item_no' => '3',
),
3 =>
array (
'item' => 'CV',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '4',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '6',
'date' => '2017-10-29',
),
),
'item_no' => '4',
),
4 =>
array (
'item' => 'NB',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '12',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '4',
'date' => '2017-10-29',
),
),
'item_no' => '5',
),
5 =>
array (
'item' => 'SP',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '4',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '6',
'date' => '2017-10-29',
),
),
'item_no' => '6',
),
6 =>
array (
'item' => 'WB',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '5',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '2',
'date' => '2017-10-29',
),
),
'item_no' => '7',
),
7 =>
array (
'item' => 'wny',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '4',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '6',
'date' => '2017-10-29',
),
),
'item_no' => '8',
),
8 =>
array (
'item' => 'bs',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '15',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '2',
'date' => '2017-10-29',
),
),
'item_no' => '9',
),
9 =>
array (
'item' => 'st',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '16',
'date' => '2017-10-29',
),
),
'item_no' => '10',
),
10 =>
array (
'item' => 'ayhtdws',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '2',
'date' => '2017-10-29',
),
),
'item_no' => '11',
),
11 =>
array (
'item' => 'sif',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '3',
'date' => '2017-10-29',
),
),
'item_no' => '12',
),
12 =>
array (
'item' => 'bb',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '6',
'date' => '2017-10-29',
),
),
'item_no' => '13',
),
)
I don't know if this is more or less the same as Erwin's code, but I wrote this code 13 hours ago and had to wait for the array.
Edit; I have tested Erwin's code and it seems we have close to matching code.
He makes one loop more to get the date in there but it's more or less the same.
I loop the array and the subarray sold.
I make the new array key the date example:
Echo $new['2017-10-28']['nk']; // 11
And if the date key is not set already I create it.
Once the loop is done I use array_values to remove the date keys making the array look like:
Echo $new[0]['nk']; // 11
The code:
$new =[];
Foreach($inventory as $sub){
Foreach($sub["sold"] as $sold){
If (!isset($new[$sold["date"]]["date"])) $new[$sold["date"]]["date"] = $sold["date"];
$new[$sold["date"]][$sub["item"]] = $sold["quantity"];
}
}
$new = array_values($new);
Var_dump($new);
https://3v4l.org/mGJSX
I've run through some tests and workarounds and come up with this solution. I've managed to do it by using ksort(), array_values()and array_key_exists(). I used the date first as an array key to contain all items sold on that date. Then, move it inside the array after gathering all items, then renumbered the array. Further explanation is given through code comments
// result array
$result = array();
// Loop for each item on inventory
foreach ($inventory as $inv) {
// Loop for each sold array
foreach ($inv['sold'] as $item) {
// date sold
$date = $item['date'];
// Check if date already exist in result array as key
// If not, Create new array with key equal to date and push to result array
if (!array_key_exists($date, $result)) {
$result[$date] = array();
}
// Add array of item => quantity to corresponding date array inside result array
$result[$date][$inv['item']] = $item['quantity'];
}
}
// From here you already have each item => quantity sold inside each date array
// e.g. $result = array( '2017-10-28' => array('NK' => '11', 'FL' => '7', ...) );
// Sort keys which are dates ascending
ksort($result);
// Loop to results and set
foreach ($result as $key => $value) {
$result[$key]['date'] = $key;
}
// Renumber Keys
$result = array_values($result);
// Print result
echo '<pre>';
print_r($result);
Output
Array
(
[0] => Array
(
[NK] => 11
[FL] => 7
[AD] => 5
[CV] => 4
[NB] => 12
[SP] => 4
[WB] => 5
[wny] => 4
[bs] => 15
[date] => 2017-10-28
)
[1] => Array
(
[NK] => 1
[FL] => 2
[AD] => 3
[CV] => 6
[NB] => 4
[SP] => 6
[WB] => 2
[wny] => 6
[bs] => 2
[st] => 16
[ayhtdws] => 2
[sif] => 3
[bb] => 6
[date] => 2017-10-29
)
)

PHP For Each Append Array

Example code array: https://eval.in/639002
A guest with (user_id = 1) bought multiple tickets (ticket_id = 1 & 2) On event (event_id = 11).
I am expecting the result format:
[ticket] => Array
(
[1] => Ticket Name, ***
[2] => Ticket Name
)
Code example below:
$data_db = array(
array(
'id' => 1,
'user_id' => 1,
'event_id' => 11,
'ticket_id' => 1,
'user_name' => 'guest 1'
),
array(
'id' => 2,
'user_id' => 2,
'event_id' => 11,
'ticket_id' => 1,
'user_name' => 'guest 2'
),
array(
'id' => 3,
'user_id' => 3,
'event_id' => 22,
'ticket_id' => 1,
'user_name' => 'guest 3'
),
array(
'id' => 4,
'user_id' => 1,
'event_id' => 11,
'ticket_id' => 2,
'user_name' => 'guest 1'
)
);
$output = [];
foreach ( $data_db as $key => $row ) {
$output[ $row['event_id'] ]['event'] = 'Event Name';
$output[ $row['event_id'] ]['attendee'][ $row['user_id'] ] = $row;
$output[ $row['event_id'] ]['attendee'][ $row['user_id'] ]['ticket'][ $row['ticket_id'] ] = 'Ticket Name';
}
print_r($output);
Current result
Array
(
[11] => Array
(
[event] => Event Name
[attendee] => Array
(
[1] => Array
(
[id] => 4
[user_id] => 1
[event_id] => 11
[ticket_id] => 2
[user_name] => guest 1
[ticket] => Array
(
[2] => Ticket Name
)
)
[2] => Array
(
[id] => 2
[user_id] => 2
[event_id] => 11
[ticket_id] => 1
[user_name] => guest 2
[ticket] => Array
(
[1] => Ticket Name ***
)
)
)
)
[22] => Array
(
[event] => Event Name
[attendee] => Array
(
[3] => Array
(
[id] => 3
[user_id] => 3
[event_id] => 22
[ticket_id] => 1
[user_name] => guest 3
[ticket] => Array
(
[1] => Ticket Name
)
)
)
)
)
First of you could start by sorting out an array of tickets of which ticket belongs to which user:
$output = [];
foreach($data_db as $key=>$row) {
// Define new $tickets Array E.g ['guest_name']=>['ticket_id']
$tickets[$row['user_name']][] = $row['ticket_id'];
$output [] = [
'id'=>$row['id'],
'user_id'=>$row['user_id'],
'event_id'=>$row['event_id'],
'user_name'=>$row['user_name'],
'tickets'=>[]
];
}
Your output of $tickets would look like:
Array
(
[guest 1] => Array
(
[0] => 1
[1] => 2
)
[guest 2] => Array
(
[0] => 1
)
[guest 3] => Array
(
[0] => 1
)
)
Then you can combine them, like this:
foreach($output as $k=>$v){
foreach($tickets as $guest=>$ticket){
if($v['user_name'] == $guest){
$output[$k]['tickets'] = $ticket;
}
}
}
Your output is:
Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[event_id] => 11
[user_name] => guest 1
[tickets] => Array
(
[0] => 1
[1] => 2
)
)
[1] => Array
(
[id] => 2
[user_id] => 2
[event_id] => 11
[user_name] => guest 2
[tickets] => Array
(
[0] => 1
)
........

PHP Remove Array Containing Empty Value within a Bigger, more complex Array

I know this might seem like a duplicate for other questions out there, but I've been testing the solutions for the other questions and I can't get them to work in my specific situation.
I have a "complex" multidimiensional array called $get_food that looks like this:
Array (
[0] => Array (
[0] => Array (
[dietID] => 562
[blockNum] => 1
)
[1] => Array (
[0] => Array (
[0] => Array (
[mealTitle] =>
)
[1] => Array (
[foodNumber] => 1
[portions] => 1
[foodID] =>
)
)
[1] => Array (
[0] => Array (
[mealTitle] =>
)
[1] => Array (
[foodNumber] => 1
[portions] => 1
[foodID] =>
)
)
)
)
)
Array (
[0] => Array (
[0] => Array (
[dietID] => 562
[blockNum] => 2
)
[1] => Array (
[0] => Array (
[0] => Array (
[mealTitle] => Carnitas
)
[1] => Array (
[foodNumber] => 1
[portions] => 1
[foodID] =>
)
)
[1] => Array (
[0] => Array (
[mealTitle] => Geometry
)
[1] => Array (
[foodNumber] => 1
[portions] => 1
[foodID] => 21
)
)
)
)
)
Array (
[0] => Array (
[0] => Array (
[dietID] => 562
[blockNum] => 3
)
[1] => Array (
[0] => Array (
[0] => Array (
[mealTitle] => Carburation
)
[1] => Array (
[foodNumber] => 1
[portions] => 1
[foodID] =>
)
[2] => Array (
[foodNumber] => 2
[portions] => 1
[foodID] =>
)
[3] => Array (
[foodNumber] => 3
[portions] => 1
[foodID] =>
)
[4] => Array (
[foodNumber] => 4
[portions] => 1
[foodID] =>
)
[5] => Array (
[foodNumber] => 5
[portions] => 1
[foodID] =>
)
)
[1] => Array (
[0] => Array (
[mealTitle] => Bar Rescue
)
[1] => Array (
[foodNumber] => 1
[portions] => 1
[foodID] =>
)
)
)
)
)
I need to iterate thru it and remove the sub arrays that have an empty [foodID].
e.g. Remove the whole sub-array with the key [5] because it has an empty [foodID] item and it's useless that way:
[5] => Array (
[foodNumber] => 5
[portions] => 1
[foodID] =>
)
And once those sub arrays are deleted, I need to reconstruct the main array so it looks the same than it did originally (with the same levels), but without the unwanted sub-arrays.
I managed to do this on a much simpler multidimensional array, but not this time around. I can't get it to look the same at the end.
This is the messy code post-frustration that I have right now that just outputs nonsense:
foreach($get_food as $key => $meal):
foreach($meal as $key1 => $mealpart){
foreach($mealpart as $key2 => $food){
//print_r($food);
foreach($food as $key3 => $fooditem){
if(($key3 != 0)&&($get_food[$key][$key1][$key2][$key3]['foodID'] == '')){
unset($get_food[$key][$key1][$key2][$key3]);
//echo 'No Food ID';
} else {
$updatedFood[$key][$key1][$key2][$key3]= $fooditem;
}
}
}
} endforeach;
So, any help is very much appreciated :)
//////// EDIT
The main array is put together from 8 arrays.
Here's the var_export for them together as one:
$get_food = array(
0 => array(
0 => array(
0 => array(
'dietID' => '562',
'blockNum' => '1',
) ,
1 => array(
0 => array(
0 => array(
'mealTitle' => '',
) ,
1 => array(
'foodNumber' => '1',
'portions' => '1',
'foodID' => '33',
) ,
) ,
1 => array(
0 => array(
'mealTitle' => '',
) ,
1 => array(
'foodNumber' => '1',
'portions' => '1',
'foodID' => '30',
) ,
) ,
2 => array(
0 => array(
'mealTitle' => '',
) ,
1 => array(
'foodNumber' => '1',
'portions' => '1',
'foodID' => '34',
) ,
) ,
) ,
) ,
) ,
1 => array(
0 => array(
0 => array(
'dietID' => '562',
'blockNum' => '2',
) ,
1 => array(
0 => array(
0 => array(
'mealTitle' => '',
) ,
1 => array(
'foodNumber' => '1',
'portions' => '1',
'foodID' => '',
) ,
) ,
) ,
) ,
) ,
2 => array(
0 => array(
0 => array(
'dietID' => '562',
'blockNum' => '3',
) ,
1 => array(
0 => array(
0 => array(
'mealTitle' => '',
) ,
1 => array(
'foodNumber' => '1',
'portions' => '1',
'foodID' => '',
) ,
) ,
) ,
) ,
) ,
3 => array(
0 => array(
0 => array(
'dietID' => '562',
'blockNum' => '4',
) ,
1 => array(
0 => array(
0 => array(
'mealTitle' => '',
) ,
1 => array(
'foodNumber' => '1',
'portions' => '1',
'foodID' => '',
) ,
) ,
) ,
) ,
) ,
4 => array(
0 => array(
0 => array(
'dietID' => '562',
'blockNum' => '5',
) ,
1 => array(
0 => array(
0 => array(
'mealTitle' => '',
) ,
1 => array(
'foodNumber' => '1',
'portions' => '1',
'foodID' => '',
) ,
) ,
) ,
) ,
) ,
5 => array(
0 => '',
) ,
6 => array(
0 => '',
) ,
7 => array(
0 => '',
) ,
);
///// EDIT 2
////////////////// SOLUTION ////////////////////
Thanks to #Brian 's help, who made me realize I was targeting the wrong keys and that I was trying to create an additional array that was not necessary I came up with the solution to this issue.
You can see the working code here:
http://codepad.org/6xgrpo46
Hope this helps someone in the future! :)
When checking if the foodID is empty or not, you are looking at the wrong value:
$get_food[$key][$key1][$key2][$key3]['foodID']
vs.
$get_food[$key][$key1][$key2][$key3][1]['foodID']
And the same problem seems to be for the unset in your example.
foreach($get_food as $key => $meal){
foreach($meal as $key1 => $mealpart){
foreach($mealpart as $key2 => $food){
foreach($food as $key3 => $fooditem){
if ($key3 == 0)
continue;
if (empty($get_food[$key][$key1][$key2][$key3][1]['foodID']))
unset($get_food[$key][$key1][$key2][$key3][1]);
}
}
}
}

How to construct sub arrays from a main array

I would like to construct sub n number of arrays from a multi dimensional array depends on the data. For example: I have a main array as
Array
(
[0] => Array
(
[id] => 1
[status] => -1
)
[1] => Array
(
[id] => 2
[status] => 1
)
[2] => Array
(
[id] => 3
[status] => 2
)
[3] => Array
(
[id] => 4
[status] => 2
)
[4] => Array
(
[id] => 5
[status] => 2
)
)
I would like to get 3 arrays from this array depends on the no and type of status value
like
array{
[0]=array(
[status]=-1
[count]=1
)
[1]=array(
[status]=1
[count]=1
)
[2]=array(
[status]=2
[count]=3
)
}
Thanks in advance,
Sunil Kumar P
You mean like this?:
$array = array(
array('id' => 1, 'status' => -1),
array('id' => 2, 'status' => 1),
array('id' => 3, 'status' => 2),
array('id' => 4, 'status' => 2),
array('id' => 5, 'status' => 2)
);
$statuses = array();
foreach($array as $one){
if(!isset($statuses[$one['status']])){ $statuses[$one['status']] = 0; }
$statuses[$one['status']]++;
}
$newArray = array();
foreach($statuses as $key => $val){
$newArray[] = array('status' => $key, 'count' => $val);
}
print_r($newArray);
/*Array
(
[0] => Array
(
[status] => -1
[count] => 1
)
[1] => Array
(
[status] => 1
[count] => 1
)
[2] => Array
(
[status] => 2
[count] => 3
)
)*/

Categories