Compare arrays and divide it based on duplicate from specific array key - php

I try to compare arrays and divide it for three different arrays based on subarray with key "ProductID"
Input data look like:
Cat Array:
Array
(
[0] => Array
(
[catID] => Array
(
[0] => 65
[1] =>66
)
[discount] => 10
[productID] => Array
(
[0] => 10887
[1] => 8508
[2] => 8056
)
[startDate] => 05/12/2022 12:00 am
[endDate] => 10/12/2022 12:00 am
)
)
Tax Array:
Array
(
[0] => Array
(
[taxID] => 2436
[discount] => 5
[productID] => Array
(
[0] => 8018
[1] => 8009
)
)
[1] => Array
(
[taxID] => 2438
[discount] => 10
[productID] => Array
(
[0] => 13184
[1] => 8009
)
)
)
When in tax array does not exists any productID from Cat Array, output for this array should look like:
$taxNoDuplicates = [
'discount' => '',
'productIds' => []
];
When in Cat Array does not exists any productID from Tax Array, output for this array should look like:
$catNoDuplicates = [
'discount' => '',
'startDate' => '',
'endDate' => '',
'productIds' => []
];
And if duplicates exsits in Cat Array and Tax Array output for this array should look like: :
$taxAndCatCombine = [
'discountTax' => '',
'discountCat' => '',
'startDate' => '',
'endDate' => '',
'productIds' => []
];
Is it possible to create output arrays the way I provided?

Related

merge two array values

How can I make this array:
Array
(
[0] => Array
(
[id] => 3412341233214
[number] => 21000
)
[1] => Array
(
[id] => 12121212121212
[number] => 18000
)
[2] => Array
(
[id] => 12121212121212
[number] => 17000
)
)
Look like the one below, where [1] and [2] have been merged into a single array based on having the same id and the number has been added together.
Array
(
[0] => Array
(
[id] => 3412341233214
[number] => 21000
)
[1] => Array
(
[id] => 12121212121212
[number] => 35000
)
)
Create a $tmp array the store the number property in it.
Only store it if the id doesn't exist yet using:
if(!isset($tmp[$obj['id']]))
To make each value unique
After that, count the $tmp number with the $array number.
$tmp[$obj['id']]['number'] += $obj['number'];
https://3v4l.org/UMJ4p
$array = array(
0 => array(
"id" => 3412341233214,
"number" => 21000
),
1 => array(
"id" => 12121212121212,
"number" => 18000
),
2 => array(
"id" => 12121212121212,
"number" => 17000
)
);
$tmp = Array();
foreach($array as $obj) {
if(!isset($tmp[$obj['id']])) {
$tmp[$obj['id']] = array_merge(Array('number'=>1),$obj);
continue;
}
$tmp[$obj['id']]['number'] += $obj['number'];
}
print_r(array_values($tmp));
The following code results into
(
[0] => Array
(
[number] => 21000
[id] => 3412341233214
)
[1] => Array
(
[number] => 35000
[id] => 12121212121212
)
)

How to check is php array have different value of key

I have this array
Array
(
[0] => Array
(
[id] => 1
[job_id] => 100
)
[1] => Array
(
[id] => 2
[job_id] => 100
)
[2] => Array
(
[id] => 3
[job_id] => 101
)
)
Now here how to check if "job_id" is different. eg all have same job_id or not and how many different job_id array have.
Thanks
Try this using array function:
$data = Array
(
[0] => Array
(
[id] => 1
[job_id] => 100
)
[1] => Array
(
[id] => 2
[job_id] => 100
)
[2] => Array
(
[id] => 3
[job_id] => 101
)
)
$totalDifferentJobId = count(array_unique(array_column($data, 'job_id')));
It will give you total unique job
Well, if it is not a too large array, you can do the following:
<?php
$arr = [
['id' => 1, 'job_id' => 100],
['id' => 2, 'job_id' => 100],
['id' => 3, 'job_id' => 101]
];
$jobids = array_unique(array_column($arr, 'job_id'));
var_dump($jobids);
This looks like a db-result, in wich case it would be better to use a group-by statement?

PHP merge arrays with same key to one custom array

I have multiple result from mysql in array like this
Array1
Array
(
[type] => Food
[storage] => S5213
[quantity1] => 1000
[in1] => 10/09/2017
[quantity2] => 1000
[in2] => 10/09/2017
)
Array 2
Array
(
[type] => Food
[storage] => S4512
[quantity1] => 990
[in1] => 13/09/2017
)
Array 3
Array
(
[type] => Drink
[storage] => K4221
[quantity1] => 12000
[in1] => 12/09/2017
)
So, i would like to merge if the key such as "type" from different arrays are same and put it into custom array. At the same time, it maybe has quantity1, quantity2, quantity3 and in1,in2,in3 for different stocks quantity and date. Would like to merge it into "rack".
Expected result
Array
(
[Food] => Array
(
[0] => Array
(
[storage] => S5213
[rack] => Array
(
[0] => Array
(
[quantity] => 1000
[in] => 10/09/2017
)
[1] => Array
(
[quantity] => 1000
[in] => 10/09/2017
)
)
)
[1] => Array
(
[storage] => S4512
[rack] => Array
(
[0] => Array
(
[quantity] => 990
[in] => 13/09/2017
)
)
)
)
[Drink] => Array
(
[0] => Array
(
[storage] => K4221
[rack] => Array
(
[0] => Array
(
[quantity] => 12000
[in] => 12/09/2017
)
)
)
)
)
Is it possible? tried using array_merge_recursive, but not output expected result.
If this is what your SQL query returns, then it looks like your database is not normalised. More concretely, you should not have columns quantity1, quantity2, ...etc in a single table. The rule is, if you have more than one of some field, create a separate table. In this case that table should just have a foreign key, maybe a sequence number (1, 2, ...) and finally a single quantity column. Multiple quantities would be expressed as multiple rows in that additional table. The in field could be added to that, as it seems to follow the same rule.
Anyway, for the given situation, you could use this PHP function:
function addArray(&$main, $arr) {
if (!is_array($main)) $main = []; // first time
$main[$arr["type"]][] = [
"storage" => $arr["storage"],
"rack" => array_reduce(array_keys($arr), function ($acc, $key) use ($arr) {
if (strpos($key, "quantity") === 0) {
$acc[] = [
"quantity" => $arr[$key],
"in" => $arr[str_replace("quantity", "in", $key)]
];
}
return $acc;
}, [])
];
}
Use it as follows:
$arr = [
"type" => "Food",
"storage" => "S5213",
"quantity1" => 1000,
"in1" => "10/09/2017",
"quantity2" => 1000,
"in2" => "10/09/2017"
];
addArray($main, $arr);
$arr = [
"type" => "Food",
"storage" => "S4512",
"quantity1" => 990,
"in1" => "13/09/2017"
];
addArray($main, $arr);
$arr = [
"type" => "Drink",
"storage" => "K4221",
"quantity1" => 12000,
"in1" => "12/09/2017"
];
addArray($main, $arr);
... etc. $main will have the desired structure.

PHP: Merge 2 arrays

I've been trying the following for a day now and can't get it to work.
I'm getting information from a paginated source (say 3 pages in this example. So I've got 3 arrays to merge:
Array
(
[status] => Active
[nrEntries] => 6
[entries] => Array
(
[0] => Array
(
[sgname] => Merc
[timeentered] => 2016-02-08 04:30:00
)
[1] => Array
(
[sgname] => bystander
[timeentered] => 2016-03-17 20:55:00
)
)
)
Array
(
[status] => Active
[nrEntries] => 6
[entries] => Array
(
[0] => Array
(
[sgname] => Elvis
[timeentered] => 2016-03-08 04:30:00
)
[1] => Array
(
[sgname] => marcAR
[timeentered] => 2016-03-07 20:55:00
)
)
)
Array
(
[status] => Active
[nrEntries] => 6
[entries] => Array
(
[0] => Array
(
[sgname] => Killer
[timeentered] => 2016-03-09 05:30:00
)
[1] => Array
(
[sgname] => MyName
[timeentered] => 2016-03-05 21:45:00
)
)
)
The result I am looking for is to merge them into 1 array that I can return as a result.
Array
(
[status] => Active
[nrEntries] => 6
[entries] => Array
(
[0] => Array
(
[sgname] => Merc
[timeentered] => 2016-02-08 04:30:00
)
[1] => Array
(
[sgname] => bystander
[timeentered] => 2016-03-17 20:55:00
)
[2] => Array
(
[sgname] => Elvis
[timeentered] => 2016-03-08 04:30:00
)
[3] => Array
(
[sgname] => marcAR
[timeentered] => 2016-03-07 20:55:00
)
[4] => Array
(
[sgname] => Killer
[timeentered] => 2016-03-09 05:30:00
)
[5] => Array
(
[sgname] => MyName
[timeentered] => 2016-03-05 21:45:00
)
)
)
The problem that I'm running into is that with array_merge it won't work because of the identical index numbers of the records.
I tried the following, but that doesn't work either.
<?PHP
// add child array to the end of $result
for ($i=0 ; $i<2; $i++) {
$result['entries'][($page*2)+$i][] = $resultChild['entries'][$i];
}
?>
$a1['entries'] = array_merge_recursive($a1['entries'], $a2['entries'], $a3['entries']);
Try this:
$array1 = array(
'status' => 'Active',
'nrEntries' => 6,
'entries' => array(
array(
'sgname' => 'Merc',
'timeentered' => '2016-02-08 04:30:00'
),
array(
'sgname' => 'bystander',
'timeentered' => '2016-03-17 20:55:00'
)
)
);
$array2 = array(
'status' => 'Active',
'nrEntries' => 6,
'entries' => array(
array(
'sgname' => 'Elvis',
'timeentered' => '2016-03-08 04:30:00'
),
array(
'sgname' => 'marcAR',
'timeentered' => '2016-03-07 20:55:00'
)
)
);
$result = array_merge_recursive($array1, $array2);
$result['status'] = array_unique($result['status'])[0];
$result['nrEntries'] = array_unique($result['nrEntries'])[0];
echo "<pre>";
print_r($result);
echo "</pre>";
This gives the following:
Array
(
[status] => Active
[nrEntries] => 6
[entries] => Array
(
[0] => Array
(
[sgname] => Merc
[timeentered] => 2016-02-08 04:30:00
)
[1] => Array
(
[sgname] => bystander
[timeentered] => 2016-03-17 20:55:00
)
[2] => Array
(
[sgname] => Elvis
[timeentered] => 2016-03-08 04:30:00
)
[3] => Array
(
[sgname] => marcAR
[timeentered] => 2016-03-07 20:55:00
)
)
)
Hope this helps.
array_merge() is not recursive so it will replace your entries array as a whole. There is a function called array_merge_recursive(), but that won't work in your case either, since it will create arrays under status and nrEntries containing the values from all arrays.
What you need to do is to merge the 'big' page arrays, and then merge the entries separately. This could look like this:
// Keep all pages in an array
$pages = [$pageOne, $pageTwo, $pageThree];
// Merge the page arrays
$result = array_merge(...$pages);
// Clear entries as they only contain the data from the last page
$result['entries'] = [];
// Merge entries with the entries of each page separately
foreach ($pages as $page) {
$result['entries'] = array_merge($result['entries'], $page['entries']);
}
This is the simplest example I could think of. I hope it helps you in understanding what is going on, so you can refactor it to suit your needs.
As long as your entries arrays have numeric keys starting from 0, array_merge will append the values instead of replacing them.

How to Add a new Key and Merge the array values in multidimemsional array PHP

here's the result of my first function:
Array
(
[0] => Array
(
[MaidID] => 13
[Stores] => Array
(
[0] => 14
[1] => 5
)
)
[1] => Array
(
[MaidID] => 3
[Stores] => Array
(
[0] => 4
)
)
[2] => Array
(
[MaidID] => 41
[Stores] => Array
(
[0] => 14
)
)
)
Then, here's the result of my second function:
Array
(
[1] => Array
(
[MaidID] => 14
[Cash] => 10000
[Debit] => 0
[Credit] => 0
)
)
and here's should be the result:
Array ([0] => Array (
[MaidID] => 14
[Cash] => 10000.00
[Debit] => 0
[Credit] => 0
[MaidsID] => Array(
[0] => 13
[1] => 41
)
)
)
Is it possible to make it?I need to a new key name MaidsID pointing to the list of MaidID owned by more than one Stores.Please help me, Please be patience in my question, im just a beginner.Thank you so much.
this code work ok. $a is your first array $b is the second, $c is result
$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('Maid' => 4, 'Stores' => array (1) ));
$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));
$MaidsID=array();
foreach ($a as $aa ){
if (count($aa['Stores']>1)){
array_push($MaidsID, $aa['Maid']);
}
}
$MaidsID=array('MaidsID' => $MaidsID);
$c = array_merge($b, $MaidsID);`
I tested it here and it was ok. (Just replace $a with your first array and $b with seccond ).
Are you sure that the structure of your arrays is exactly as you have written above?? Maybe there is any problem with that.
You have puted array inside another array? (its not needed i think)
Howerver: For this code:
`$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('Maid' => 4, 'Stores' => array (1) ));
$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));
print_r($a);
echo "<br><br>================================================<br><br>";
print_r($b);
echo "<br><br>================================================<br><br>";
$MaidsID=array();
foreach ($a as $aa ){
if (count($aa['Stores']>1)){
array_push($MaidsID, $aa['Maid']);
}
}
$MaidsID=array('MaidsID' => $MaidsID);
$c = array_merge($b, $MaidsID);
print_r($c);
echo "<br><br>================================================<br><br>";`
The output is:
Array ( [0] => Array ( [Maid] => 1 [Stores] => Array ( [0] => 1 [1] => 5 ) ) [1] => Array ( [Maid] => 3 [Stores] => Array ( [0] => 4 ) ) [2] => Array ( [Maid] => 4 [Stores] => Array ( [0] => 1 ) ) )
================================================
Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) )
================================================
Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) [MaidsID] => Array ( [0] => 1 [1] => 3 [2] => 4 ) )
================================================
Isn't this how you want the result?
Have a look at this. Maybe this can help you.
$result = array_merge($array1, $array2);

Categories