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
)
)
Related
i have two separate arrays $first_one:
Array
(
[0] => Array
(
[_date] => 2019-10-16
[_number] => 1
[_order] => 1
[name] => jack
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array
(
[_date] => 2020-10-11
[_number] => 2
[_order] => 2
[name] => joey
[other_ids] => Array
(
[b_id] => 1433
)
)
)
and the $second_array:
Array
(
[0] => Array
(
[date] => 2019-10-16
[number] => 1
[order] => 1
[name] => jack
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array
(
[date] => 2020-10-11
[number] => 2
[order] => 2
[name] => joey
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1433
)
)
[2] => Array
(
[date] => 2020-10-28
[number] => 3
[order] => 3
[name] => tom
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1593
)
)
)
they are very similar but they came from different api's and they are different in numbers and also some key names.
so what i'm trying to do is to count the $first_one arrays and if for that many loop through the $second_one (in this example is 2) and if the [_number] [_order] from $first_one was equal (==) to [number] [number] $second_one then take some info (for example the last name) from it and put in a new array.
so is this possible to do?
$arr1 = [ [ '_date' => '2019-10-16','_number' => 1,'_order' => 1,
'name' => 'jack','other_ids' => ['b_id' => 1253]
],
['_date' => 2020-10-11,'_number' => 2,'_order' => 2,
'name' => 'joey','other_ids' => ['b_id' => 1433]
]
];
$arr2 = [ [ 'date' => '2019-10-16','number' => '1','order' => '1',
'name' => 'jack','last_name' => 'foobar','other_ids' => ['b_id' => 1253]
],
[ 'date' => '2019-10-11','number' => '2','order' => '2',
'name' => 'joey','last_name' => 'foobar','other_ids' => ['b_id' => 1433]
],
[ 'date' => '2019-10-28', 'number' => '3', 'order' => '3',
'name' => 'tom', 'last_name' => 'foobar', 'other_ids' => ['b_id' => 1593]
],
];
// first make second array more directly searchable
// make new array with the `number` as the key
foreach( $arr2 as $a){
$arr2new[$a['number']] = $a;
}
foreach ($arr1 as $a) {
if ( array_key_exists($a['_number'], $arr2new) &&
$a['_order'] == $arr2new[$a['_order']]['order'] )
{
$merged[] = ['name'=>$a['name'], 'other_ids' => $a['other_ids']];
}
}
print_r($merged);
RESULT
Array
(
[0] => Array (
[name] => jack
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array (
[name] => joey
[other_ids] => Array
(
[b_id] => 1433
)
)
)
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
)
)
)
)
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
)
........
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,
)*/
How can I remove duplicate values from a multi-dimensional array in PHP?
Initial array:
array (
0 =>
array (
'following_userid' => '88',
),
1 =>
array (
'following_userid' => '89',
),
2 =>
array (
'following_userid' => '287',
),
3 =>
array (
'following_userid' => '346',
),
4 =>
array (
'following_userid' => '405',
),
5 =>
array (
'following_userid' => '284',
),
6 =>
array (
'following_userid' => '583',
),
7 =>
array (
'following_userid' => '587',
),
8 =>
array (
'following_userid' => '655',
),
9 =>
array (
'following_userid' => '95',
),
10 =>
array (
'follower_userid' => '89',
),
11 =>
array (
'follower_userid' => '88',
),
12 =>
array (
'follower_userid' => '353',
),
13 =>
array (
'follower_userid' => '42',
),
14 =>
array (
'follower_userid' => '626',
),
15 =>
array (
'follower_userid' => '655',
),
16 =>
array (
'follower_userid' => '95',
),
)
As per the How to remove duplicate values from a multi-dimensional array in PHP Suggestion i used $input = array_map("unserialize", array_unique(array_map("serialize", $input)));
Array After performing array_unique():
array (
0 =>
array (
'following_userid' => '88',
),
1 =>
array (
'following_userid' => '89',
),
2 =>
array (
'following_userid' => '287',
),
3 =>
array (
'following_userid' => '346',
),
4 =>
array (
'following_userid' => '405',
),
5 =>
array (
'following_userid' => '284',
),
6 =>
array (
'following_userid' => '583',
),
7 =>
array (
'following_userid' => '587',
),
8 =>
array (
'following_userid' => '655',
),
9 =>
array (
'following_userid' => '95',
),
10 =>
array (
'follower_userid' => '89',
),
11 =>
array (
'follower_userid' => '88',
),
12 =>
array (
'follower_userid' => '353',
),
13 =>
array (
'follower_userid' => '42',
),
14 =>
array (
'follower_userid' => '626',
),
15 =>
array (
'follower_userid' => '655',
),
16 =>
array (
'follower_userid' => '95',
),
)
But Still getting Duplicate answers. Seems to have no effect on the original array.
First of all, since 5.2.9 you can use the much simpler version, taken from this answer:
array_unique($array, SORT_REGULAR);
In this case, array_unique() actually gives the correct output; the thing here is that you have two different keys in your array: "follower_userid" and "following_userid", so to get the unique id's regardless of the keys, you have to normalize it first:
array_map('current', $array);
Doing this takes the first element of each array and creates a new array with just the value of that first element.
Output:
Array
(
[0] => 88
[1] => 89
[2] => 287
[3] => 346
[4] => 405
[5] => 284
[6] => 583
[7] => 587
[8] => 655
[9] => 95
[10] => 89
[11] => 88
[12] => 353
[13] => 42
[14] => 626
[15] => 655
[16] => 95
);
And then apply array_unique():
array_unique(array_map('current', $array));
If it's always only going to have just the follower_userid, you could just flatten the array down with something like this:
$followers = array();
foreach ($input as $item) {
$followers[] = $item['followers_userid'];
}
$followers = array_unique($followers);