I would like to subtract the quantity of $array2 from the stocks of $array1.
$array1= ([product_id]=>4, [stocks]=>20)
$array2= ([product_id]=>4, [quantity]=>3)
So that would be:
$array1= ([0]=> 4, [1] => 20);
$array2= ([0]=> 4, [1] => 3);
And then the output should be:
$array1= ([0]=> 4, [1] => 17);
Your array structure looks slightly different with multiple records, the code works out like this in an ugly manner. I'm assuming you're talking about something like this:
$array1 = array(
0=>array('product_id'=>4, 'stocks'=>20),
1=>array('product_id'=>5, 'stocks'=>60));
$array2 = array(
0=>array('product_id'=>4, 'quantity'=>3)
1=>array('product_id'=>5, 'quantity'=>30));
...It's a multi-dimensional array (typical for records pulled from a database).
foreach($array1 as $key=>$value){
foreach($array2 as $key2=>$value2) {
if($value['product_id']==$value2['product_id']){
$value['stocks'] -= $value2['quantity'];
//optimization to avoid searching this again.
unset($array2[$key]);
}
}}
With what you have given the following will do what you are asking for:
if($array1['product_id'] == $array2['product_id']) {
$array1['stocks'] -= $array2['quantity'];
}
If you need to loop through a bigger array then what I have given is only part of the larger puzzle.
Jesse's answer wasn't tested and will not provide the desired output because the "stocks" array wasn't being modified -- a copy of the array was being modified in the loop -- so if you try to print the result to screen, there would be no change.
To modify by reference, use & just before the value variable in the first loop.
Also the unset() key must come from the inner loop to be accurate.
Additionally, if the "sales" "product_id"s are unique, then breaking the inner loop upon matching will improve performance. (This is how array_search() works.)
Code: (Demo)
$stocks = [
['product_id'=>2, 'stocks'=>50],
['product_id'=>3, 'stocks'=>100],
['product_id'=>4, 'stocks'=>20],
['product_id'=>5, 'stocks'=>60]
];
$sales = [
['product_id'=>4, 'quantity'=>3],
['product_id'=>5, 'quantity'=>30]
];
foreach ($stocks as &$row) { // modify by reference
foreach ($sales as $k => $row2) { // search for product_id match
if ($row['product_id'] == $row2['product_id']) {
$row['stocks'] -= $row2['quantity']; // subtract
unset($sales[$k]); // eliminate match from lookup array
break; // assuming $sales['product_id'] values are unique
}
}
}
var_export($stocks);
Output:
array (
0 =>
array (
'product_id' => 2,
'stocks' => 50,
),
1 =>
array (
'product_id' => 3,
'stocks' => 100,
),
2 =>
array (
'product_id' => 4,
'stocks' => 17,
),
3 =>
array (
'product_id' => 5,
'stocks' => 30,
),
)
Alternatively, you can converted the sales array into a flattened, product_id-keyed array to serve as a lookup.
Code: (Demo)
$keyed = array_column($sales, 'quantity', 'product_id');
var_export($keyed);
echo "\n---\n";
foreach ($stocks as &$row) { // modify by reference
if (isset($keyed[$row['product_id']])) { // search for product_id match
$row['stocks'] -= $keyed[$row['product_id']]; // subtract
}
}
var_export($stocks);
Output:
array (
4 => 3,
5 => 30,
)
---
array (
0 =>
array (
'product_id' => 2,
'stocks' => 50,
),
1 =>
array (
'product_id' => 3,
'stocks' => 100,
),
2 =>
array (
'product_id' => 4,
'stocks' => 17,
),
3 =>
array (
'product_id' => 5,
'stocks' => 30,
),
)
Related
I have an array
$data = array(
1 => array(1 => 11 ,2 => 21, 3 => 31),
2 => array(1 => 21 ,2 => 22),
3 => array(1 => 31 ,2 => 23, 3 => 32),
);
which I want to transform to
$data = array(
1 => array(1 => 11 ,2 => 21, 3 => 31, 'Total' => 63),
2 => array(1 => 21 ,2 => 22, 'Total' => 43),
3 => array(1 => 31 ,2 => 23, 3 => 32, 'Total' => 83),
'Total' => array(1 => 63,2 => 46,3 => 63, 'Total' => 172),
);
the keys 1,2,3 are dynamic these could be 1 to 10 and also for sub array. Can you help ?
Most of the answers in Stack Overflow that I could find are related to either sum the elements within an array or the sum of different keys within a multi-dimensional array, but I couldn't find any that addressed both in the same code, so I thought it might be useful to share.
Premises
Your desired result seems incorrect, by manually doing the calculations the totals should be different than what you described. So I am assuming you just got your example wrong and this is the actual result you want:
$data = array(
1 => array(1 => 11 ,2 => 21, 3 => 31, 'Total' => 63),
2 => array(1 => 21 ,2 => 22, 'Total' => 43),
3 => array(1 => 31 ,2 => 23, 3 => 32, 'Total' => 86),
'Total' => array(1 => 63,2 => 66,3 => 63, 'Total' => 192),
);
Loop and sum arrays
So you basically want two things:
For each of the first-level elements of your arrays, you want to calculate the Total. This is done with array_sum and you can apply it by doing a foreach on the first level of your $data array. More info on array_sum and the official php documentation.
Then for each of the second-level elements, you want to sum the ones with the same key. To do so, you can use a simple foreach that keeps track of the second-level key and sums each value
Resulting Code
The resulting code combines these two points with the following logic:
Initialize the Totals key in the first-level of your $data array
Loop the first-level elements
For each first-level element, use array_sum to get the Total
Loop the second-level elements
For each Index of the second level (1, 2, 3), initialize the same index in the Totals array
For each Index of the second level (1, 2, 3), sum the counter (11, 21, 31) in the Totals array
I've added comments to make it clearer:
// Initialize the 'Total' key in the first-level of the array. This will be populated in the foreach
$data['Total'] = [];
// Loop the first level of the array
foreach ($data as $key => $values) {
// Calculate the sum of the first level of the array, using array_sum
$data[$key]['Total'] = array_sum($data[$key]);
// Loop the second level of the array
foreach ($values as $index => $count) {
// For each INDEX, create the corresponding element of the Totals array
if (!isset($data['Total'][$index])) {
$data['Total'][$index] = 0;
}
// Sum this element $count in the Total array
$data['Total'][$index] += $count;
}
}
This question already has answers here:
Filter/Remove rows where column value is found more than once in a multidimensional array
(4 answers)
Closed 9 months ago.
I have the following array dynamically generated from an external API:
$purchases = array(
array('product_id' => 7, 'receipt' => R13D13),
array('product_id' => 5, 'receipt' => Y28Z14),
array('product_id' => 7, 'receipt' => R02310),
array('product_id' => 5, 'receipt' => E11403)
);
Desired output:
$purchases_new = array(
array('product_id' => 7, 'receipt' => R13D13),
array('product_id' => 5, 'receipt' => Y28Z14)
);
Due to the nature of the API I have only been able to pull data using array_map:
$purchases_unique = array_unique(
array_map(function($pid){ return $pid['product_id']; }, $purchases)
);
print_r($purchases_unique);
Outputs
array((array[251] => 7) array([252] => 5))
Output completely not what I want :( How to array_map 2 columns product_id' and receipt and remove duplicate rows based on product_id?
I want to achieve the following:
array_map both columns? (product_id and receipt)
Remove product_id duplicates rows based on product_id key?
If you want the first occurrence use #TheFirstBird answer. If you want the last occurrence you can do it simpler buy using array_column and array_values as:
$newArr = array_values(array_column($purchases, null, "product_id"));
Notice the second argument of array_column is null meaning take the entire element.
Live example: 3v4l
References: array-column, array-values
I think this will do what you're looking for:
Just adds the unique product_ids (with receipt) to a new array:
$purchases_new = array_reduce($purchases, function($acc, $p) {
if(!in_array($p['product_id'], array_column($acc, 'product_id'))) {
$acc[] = $p;
}
return $acc;
}, []);
Output:
array (
0 =>
array (
'product_id' => 7,
'receipt' => 'R13D13',
),
1 =>
array (
'product_id' => 5,
'receipt' => 'Y28Z14',
),
)
Try it online!
As per the comment It does not matter if it's the first or last occurrence, you could use a foreach and create a $result array for the non duplicates.
For every iteration, use the product_id as the array key and check if it already has been set.
If it is not, then add it.
$purchases = array(
array('product_id' => 7, 'receipt' => "R13D13"),
array('product_id' => 5, 'receipt' => "Y28Z14"),
array('product_id' => 7, 'receipt' => "R02310"),
array('product_id' => 5, 'receipt' => "E11403")
);
$result = [];
foreach($purchases as $key => $purchase) {
if (!isset($result[$purchase["product_id"]])) {
$result[$purchase["product_id"]] = $purchase;
}
}
print_r($result);
Result
Array
(
[7] => Array
(
[product_id] => 7
[receipt] => R13D13
)
[5] => Array
(
[product_id] => 5
[receipt] => Y28Z14
)
)
Php demo
I have these two arrays as output:
Value Array
(
[0] => 10100153
[1] => 2007
[2] => 350
[3] => 804082
[4] => WW006
[5] => WHT/NNY/OXGM
[6] => 35/38
[7] => 804082 WW00635/38
[8] => 0,00138857
[9] => Champion 3pk Quarter Socks
)
Numbers Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
I want to combine them and change the key value of the value array in value and the numbers array in numbers, so it looks something like this:
Desire output
['Value' => '10100153', 'Number' => 1],
['Value' => '2007', 'Number' => 2],
['Value' => '390', 'Number' => 3],
['Value' => '804715', 'Number' => 4],
['Value' => 'WW001', 'Number' => 5],
['Value' => 'WHT/WHT/WHT', 'Number' => 6],
['Value' => '39/42', 'Number' => 7],
['Value' => '804715 WW00139/42', 'Number' => 8],
['Value' => '0.00138857', 'Number' => 9],
['Value' => '3pk Quarter Socks', 'Number' => 10]
All I can find is array_combine and array_merge, but array_merge just adds the numbers array to the end of the value array, and array_combine adds the numbers to the end of the text of the value array
You can use array_map (doc) and array_combine (doc) as:
$res = array_map(null, $valuesArray, $numbersArray);
$keys = array("Value", "Number");
$res = array_map(function ($e) use ($keys) {return array_combine($keys, $e);}, $res);
Notice the use of null in array_map. From documentation:
An interesting use of this function is to construct an array of arrays, which can be easily performed by using NULL as the name of the callback function
This way you can merge more arrays - just remember to add the correct key to $keys
Live example: 3v4l
You could use a regular foreach loop to iterate over your values array. At each element in the values array you can get its corresponding element in the numbers array by using the current index.
At each iteration (each loop of your values array) you can add an associative array into a resulting array (here I called it $res).
See example below:
$values = ["10100153", "2007", "350", "804082", "WW006", "WHT/NNY/OXGM", "35/38", "804082 WW00635/38", "0,00138857", "Champion 3pk Quarter Socks"];
$nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$res = []; // create empty array to hold associative arrays
foreach($values as $i=>$val) { // loop over your values array, where the index is $i and the value is $val
$num = $nums[$i]; // get the number at the given index
$res[$i] = ["Value" => $val, "Number" => $num]; // set the index in the resulting array to hold a newly formed associative array
}
print_r($res); // print the results
You can skip the extra round of iterating (while transposing with array_map(null...)) as demonstrated in dWinder's answer by passing both input arrays into array_map() and using the splat operator to receive the arguments inside the function. My snippet will have half of the computational complexity of dWinder's solution -- just one loop instead of two.
Code: (Demo) (or with compact())
$values = ["10100153", "2007", "350", "804082", "WW006", "WHT/NNY/OXGM", "35/38", "804082 WW00635/38", "0,00138857", "Champion 3pk Quarter Socks"];
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$keys = ["Value", "Number"];
var_export(
array_map(function(...$data) use ($keys) {
return array_combine($keys, $data);
}, $values, $numbers)
);
Output:
array (
0 =>
array (
'Value' => '10100153',
'Number' => 1,
),
1 =>
array (
'Value' => '2007',
'Number' => 2,
),
2 =>
array (
'Value' => '350',
'Number' => 3,
),
...you get the point
)
How can I use the php array_multisort to sort arrays like this? I can't find any examples with this type of arrays. I have tried different avenues but I keep getting the error array_multisort(): Array sizes are inconsistent.
$array= Array (
"Arnold" => Array ( "index" => 2, "games_played" => 1, "score" => 5 ),
"Chris" => Array ( "index" => 1, "games_played" => 1, "score" => 5 ),
"Mike" => Array ( "index" => 0, "games_played" => 2, "score" => 5 )
);
I think you're taking it the wrong way. array_multisort is not what would be a "sort by" in other languages (i.e: sort array elements by some properties), instead it sorts the first array, and reverberate that order to all following arrays. And in case of equality it checks the corresponding values of the second arrays, etc...
If you want to order your example by score (desc), then by game played, then by index (and then by name, but this should never happen since indexes are uniques) you should do:
$array= Array (
"Arnold" => Array ( "index" => 2, "games_played" => 1, "score" => 5 ),
"Chris" => Array ( "index" => 1, "games_played" => 1, "score" => 5 ),
"Mike" => Array ( "index" => 0, "games_played" => 2, "score" => 5 )
);
$names = [];
$indexes = [];
$games_played = [];
$scores = [];
foreach ($array as $name => $player) {
$names[] = $name;
$indexes[] = $player['index'];
$games_played[] = $player['games_played'];
$scores[] = $player['score'];
}
array_multisort(
$scores, SORT_DESC,
$games_played,
$indexes,
$names,
$array /* This line will sort the initial array as well */
);
I would like to merge the associative elements from my second array into my first array where the second array's subarray key matches a row's epg_channel_id value.
First array:
[
[
'num' => 1,
'name' => 'name 1',
'epg_channel_id' => 'ch111',
'added' => '1505435915',
],
[
'num' => 2,
'name' => 'name 2',
'epg_channel_id' => 'ch222',
'added' => '1505435915',
],
[
'num' => 3,
'name' => 'name 3',
'epg_channel_id' => 'ch333',
'added' => '1505435915',
],
[
'num' => 4,
'name' => 'name 4',
'epg_channel_id' => 'ch444',
'added' => '1505435915'
]
]
And the second array:
[
['ch000' => 'Um9jayBJbiBSaW8='],
['ch111' => 'Um9jayBJbiBSaW8='],
['ch222' => 'Um9jayBJbiBSaW8='],
['ch333' => 'Um9jayBJbiBSaW8='],
['ch444' => 'Um9jayBJbiBSaW8=']
]
Desired output (for one row):
Array
(
[0] => Array
(
[num] => 1
[name] => name 1
[epg_channel_id] => ch111
[added] => 1505435915
[ch111] => Um9jayBJbiBSaW8=
)
...
)
I tried array_recursive, array merge and not works.
If the corresponding indexes in both arrays are guaranteed to have the same channel id, this will work quite efficiently. For example, if $array1[0] is guaranteed to have the same channel id as $array2[0] this solution will work nicely:
$combined = [];
foreach($array1 as $key=>$val){
$combined[$key] = $val + $array2[$key];
}
However, if the corresponding indexes are not guaranteed to have the same channel ids, this solution will not work, and you'll need to use one of the other posted answers.
One last note if you do use this method is that if the arrays are different sizes, you will want the largest one to be $array1. So, just do a comparison to see which has the most elements.
You have to loop over two arrays to get desired result: as you have to match epg_channel_id of first array to second arrays inner key
$arr1 = Array
(
0 => Array
(
"num" => 1,
"name" => "name 1",
"epg_channel_id" => "ch111",
"added" => "1505435915",
),
1 => Array
(
"num" => 2,
"name" => "name 2",
"epg_channel_id" => "ch222",
"added" => "1505435915",
),
2 => Array
(
"num" => 3,
"name" => "name 3",
"epg_channel_id" => "ch333",
"added" => "1505435915",
),
3 => Array
(
"num" => 4,
"name" => "name 4",
"epg_channel_id" => "ch444",
"added" => "1505435915",
),
);
$arr2 = Array
(
0 => Array
(
"ch000" => "Um9jayBJbiBSaW8="
),
1 => Array
(
"ch111" => "Um9jayBJbiBSaW8="
),
2 => Array
(
"ch222" => "Um9jayBJbiBSaW8="
),
3 => Array
(
"ch333" => "Um9jayBJbiBSaW8="
),
4 => Array
(
"ch444" => "Um9jayBJbiBSaW8="
),
);
$new_array = array();
foreach($arr1 as $key=>$value)
{
foreach($arr2 as $key1=>$value1)
{
foreach($value1 as $key2=>$value2)
{
if($key2 == $value['epg_channel_id'])
{
$value[$key2]=$value2;
}
}
}
$new_array[$key]=$value;
}
print_r($new_array);
DEMO
You can key exists or not using array_key_exists in second array then add it to new array
Working Demo: https://eval.in/863359
$array = Array
(
Array
(
'num' => 1,
'name' => 'name 1',
'epg_channel_id' => 'ch111',
'added' => '1505435915',
),
Array
(
'num' => 2,
'name' => 'name 2',
'epg_channel_id' => 'ch222',
'added' => '1505435915',
),
Array
(
'num' => 3,
'name' => 'name 3',
'epg_channel_id' => 'ch333',
'added' => '1505435915',
),
Array
(
'num' => 4,
'name' => 'name 4',
'epg_channel_id' => 'ch444',
'added' => '1505435915'
)
);
$array2 = Array
(
Array
(
'ch000' => 'Um9jayBJbiBSaW8='
),
Array
(
'ch111' => 'Um9jayBJbiBSaW8='
),
Array
(
'ch222' => 'Um9jayBJbiBSaW8='
),
Array
(
'ch333' => 'Um9jayBJbiBSaW8='
),
Array
(
'ch444' => 'Um9jayBJbiBSaW8='
)
);
$newArray =[];
foreach ($array as $key => $value) {
foreach ($array2 as $key2 => $value2) {
if (array_key_exists($value['epg_channel_id'], $value2)) {
$value[$value['epg_channel_id']] = $value2[$value['epg_channel_id']];
}
}
$newArray[] = $value;
}
echo "<pre>";
print_r($newArray);
array_merge_recursive works well for associated array that have keys are string. Numeric keys will be appended. From php.net
If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.
You have to convert your array to string keys, or using one loop to merge child arrays one by one.
Try this . I hope it will solve your problems. I have tested it.
foreach ($array1 as $key => $value){
// echo $key;
foreach ($array2 as $i =>$item){
foreach ($item as $j=>$subitem){
if($value['epg_channel_id'] == $j){
$array1[$key][$j] = $subitem;
}
}
}
}
print_r($array1);
try to read the value of 'epg_channel_id' from array1
and insert it to array1 itself from getting 'ch111' from array2
$ch_name = $array1[$i]['epg_channel_id'];
$id = $array1[$i]['num'];
$ch_value = $array2[$id]['$ch_name'];
$array1[$i]["$ch_name"] = $ch_value;
try to put in foreach for every array
$new_arr = [];
foreach($arr1 as $val){
foreach($arr2 as $val2){
if(array_key_exists($val['epg_channel_id'], $val2)){
$val[$val['epg_channel_id']] = $val2[$val['epg_channel_id']];
break;
}
}
$new_arr[] = $val;
}
print_r($new_arr);
The bad news is that your second array is not suitably structured to serve as a lookup array. The good news is that the step to flatten the structure into a simple associative array is quite easy.
Once the lookup is declared, just use a loop and modify-by-reference as you use array union syntax to append the desired key-value pairs.
I do not recommend any answers that are using nested loops -- they will not perform efficiently.
Code: (Demo)
$lookup = array_merge(...$array2);
foreach ($array as &$row) {
$row += [$row['epg_channel_id'] => $lookup[$row['epg_channel_id']]];
}
var_export($array);