Add all duplicates element in php array - php

Input array:
Array([0] => Array
(
[id] => 1
[check] => 1
[option] => "mk"
)[1] => Array
(
[id] => 2
[check] => 3
[option] => "zz"
)[2] => Array
(
[id] => 3
[check] => 5
[option] => "mk"
))
Output array:
Array([0] => Array
(
[id] => 1
[check] => 6
[option] => "mk"
)[1] => Array
(
[id] => 2
[check] => 3
[option] => "zz"
))
How to add all values [check] for all same [option] values and simplifiy an output array by removing already added arrays?

You just need to use foreach loop.
Here is the solution.
<?php
$arr = [
["id" => 1, "check" => 1, "option" => "mk"],
["id" => 2, "check" => 3, "option" => "zz"],
["id" => 3, "check" => 5, "option" => "mk"]
];
$newArr = [];
foreach($arr as $a) {
// check if array element is already there based on option key in $newArr. In that case just add check value.
if(isset($newArr[$a['option']])) {
$newArr[$a['option']]['check'] += $a['check'];
continue;
}
$newArr[$a['option']] = $a;
}
$newArr = array_values($newArr);
print_r($newArr);
Here is the output.
Array
(
[0] => Array
(
[id] => 1
[check] => 6
[option] => mk
)
[1] => Array
(
[id] => 2
[check] => 3
[option] => zz
)
)

Related

Append one array values as key value pair to another array php

I have two arrays both guaranteed to be the same length. The two arrays are of the following structures
array1
Array
(
[0] => Array
(
[id] => 841052
[store] => 11
[position] => 1
)
[1] => Array
(
[id] => 1613197
[store] => 11
[position] => 401
)
[2] => Array
(
[id] => 1648966
[store] => 11
[position] => 1
)
[3] => Array
(
[id] => 1656857
[store] => 11
[position] => 1
)
....
....
)
array2
Array
(
[0] => 5/20/2019
[1] => 7/7/2019
[2] => 12/16/2018
...
...
)
How do I append every value of array2 as a key value pair to array1 to get the following array. The key name can be whatever I just chose date.
Array
(
[0] => Array
(
[id] => 841052
[store] => 11
[position] => 1
[date] => 5/20/2019
)
[1] => Array
(
[id] => 1613197
[store] => 11
[position] => 401
[date] => 7/7/2019
)
[2] => Array
(
[id] => 1648966
[store] => 11
[position] => 1
[date] => 12/16/2018
)
)
...
...
...
I have tried
array_push($array1, $array2);
It just pushed it to the last element of the array. I thought of using two foreach loops but couldn't get ti to work. Is there a built in php function that will do this, or do I have to do it in loops.
Just walk $array1 and modify each sub-array by adding the new key and the value of $array2 with the same key:
array_walk($array1, function(&$v, $k) use($array2) { $v['date'] = $array2[$k]; });
try this:
$array1 = array(array("id" => 841052, "store" => "11", "position" => "1"), array("id" => 1613197, "store" => "11", "position" => "401"),);
$array2 = array("5/20/2019", "7/7/2019");
foreach ($array1 as $index => $valuearray1) {
if (array_key_exists($index, $array2)) {
$array1[$index]["date"] = $array2[$index];
}
}
var_dump($array1);

How add a new a key after using USORT in PHP multidimensional array

I am having one array in that array i want to make ASC order, after that i want insert new column rank then value it will get increase like 1, 2, 3 ...
My Array
$mainArray = [
"key1" => ["name" => "A", "price" => 5],
"key2" => ["name" => "B", "price" => 7],
"key3" => ["name" => "C", "price" => 2],
];
My Code
usort($mainArray, function($a, $b) {
return $a['price'] <=> $b['price'];
});
echo "<pre>";
print_r($mainArray);
I am getting output
Array
(
[0] => Array
(
[name] => C
[price] => 2
)
[1] => Array
(
[name] => A
[price] => 5
)
[2] => Array
(
[name] => B
[price] => 7
)
)
Expected output
Array
(
[0] => Array
(
[name] => C
[price] => 2
[rank] => 1
)
[1] => Array
(
[name] => A
[price] => 5
[rank] => 2
)
[2] => Array
(
[name] => B
[price] => 7
[rank] => 3
)
)
Since your array has been sorted and re-indexed, the rank value is simply the key plus 1. A foreach loop will do what you want:
foreach ($mainArray as $k => &$v) {
$v['rank'] = $k + 1;
}
Demo on 3v4l.org

Array Comparison in Php, and find the diff in values

I want to compare two arrays in php. My arrays looks like this
Array (
[0] => Array ( [Id] => 1 [row1] => 1458)
[1] => Array ( [Id] => 2 [row1] => 16)
[2] => Array ( [Id] => 3 [row1] => 115)
[3] => Array ( [Id] => 4 [row1] => 18)
[4] => Array ( [Id] => 5 [row1] => 13)
[5] => Array ( [Id] => 6 [row1] => 13)
[6] => Array ( [Id] => 7 [row1] => 131)
)
Array (
[0] => Array ( [Id] => 1 [row1] => 158)
[1] => Array ( [Id] => 2 [row1] => 165)
[2] => Array ( [Id] => 3 [row1] => 111)
[3] => Array ( [Id] => 4 [row1] => 186)
[4] => Array ( [Id] => 5 [row1] => 3)
)
Firstly, array1 size and array2 sizes were not equal always. Id value in array1 may or may not present in array2, If the value is not present, function have to print the total index in array3, like
[someindex] => Array ( [Id] => 6 [row1] => 13 )
if it is present, function should subtract the row1 of array1 to row1 of array2 and print in array3, like this
[someindex] => Array ( [Id] => 1 [row1] => 1300)
and my final output should be,
Array (
[0] => Array ( [Id] => 1 [row1] => 1300)
[1] => Array ( [Id] => 2 [row1] => -149)
[2] => Array ( [Id] => 3 [row1] => 4)
[3] => Array ( [Id] => 4 [row1] => -168)
[4] => Array ( [Id] => 5 [row1] => 10)
[5] => Array ( [Id] => 6 [row1] => 13)
[6] => Array ( [Id] => 7 [row1] => 131)
)
Can any one help me in solving this problem.
$arr1 = Array (
0 => Array ('Id' => 1, 'row1' => 1458)
,1 => Array ('Id' => 2, 'row1' => 16)
,2 => Array ('Id' => 3, 'row1' => 115)
,3 => Array ('Id' => 4, 'row1' => 18)
,4 => Array ('Id' => 5, 'row1' => 13)
,5 => Array ('Id' => 6, 'row1' => 13)
,6 => Array ('Id' => 7, 'row1' => 131)
);
$arr2 = Array (
0 => Array('Id' => 1, 'row1' => 158)
,1 => Array('Id' => 2, 'row1' => 165)
,2 => Array('Id' => 3, 'row1'=> 111)
,3 => Array('Id' => 4, 'row1' => 186)
,4 => Array('Id' => 5, 'row1' => 3)
);
$final = array();
foreach($arr1 as $k => $sec)
{
$sub = 0;
foreach($arr2 as $sec2)
{
if($sec2['Id']==$sec['Id'])
{
$sub = $sec2['row1'];
break;
}
}
$sec['row1'] -= $sub;
//Or to save to another element:
//$sec['row2'] = $sec['row1']-$sub;
$final[] = $sec;
}
echo '<pre>'.print_r($final,true).'</pre>';
Output:
Array (
[0] => Array ( [Id] => 1 [row1] => 1300)
[1] => Array ( [Id] => 2 [row1] => -149)
[2] => Array ( [Id] => 3 [row1] => 4)
[3] => Array ( [Id] => 4 [row1] => -168)
[4] => Array ( [Id] => 5 [row1] => 10)
[5] => Array ( [Id] => 6 [row1] => 13)
[6] => Array ( [Id] => 7 [row1] => 131)
)
First, you either have to make the second array searchable by using the Id value as the keys or write a search function. I'm choosing the former:
$searchable = array_reduce($array2, function(&$result, $item) {
return $result + array($item['Id'] => $item['row1']);
}, array());
The array_reduce() function starts with an empty array and builds it using the array addition operator; this creates an array that can be dereferenced using the id.
Then you perform a map operation on the first array:
$array3 = array_map(function($item) use ($searchable) {
$value = isset($searchable[$item['Id']]) ? $searchable[$item['Id']] : 0;
$item['row1'] -= $value;
// $item['row2'] = $item['row1'] - $value;
return $item;
}, $array1);
Doing a map operation preserves the original array and creates a new one with the values you choose inside a callback function.
Don't know if this exactly what you want, but you can check if a key exists with array_key_exists:
$array3 = array();
foreach ($array1 as $k => $v) {
if (array_key_exists($k, $array2)) {
$array3[] = $v - $array2[$k];
} else {
$array3[] = $v;
}
}
This creates an indexing array ($new) in case the main keys don't match of in the original arrays.
$arr = array (
0 => array ( 'Id' => '1','row1' => 1458),
1 => array ( 'Id' => '2','row1' => 16),
2 => array ( 'Id' => '3','row1' => 115),
3 => array ( 'Id' => '4','row1' => 18),
4 => array ( 'Id' => '5','row1' => 13),
5 => array ( 'Id' => '6','row1' => 13),
6 => array ( 'Id' => '7','row1' => 131));
$arr2 = array (
0 => array ( 'Id' => '1','row1' => 158),
1 => array ( 'Id' => '2','row1' => 165),
2 => array ( 'Id' => '3','row1' => 111),
3 => array ( 'Id' => '4','row1' => 186),
4 => array ( 'Id' => '5','row1' => 3));
$new = array();
foreach ($arr2 as $key => $value){
$new[$value['Id']] = $value['row1'];
}
foreach ($arr as $key => $value){
if (isset($new[$value['Id']])){
$arr[$key]['row1'] -= $new[$value['Id']];
}
}
print_r($arr);

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);

Remove duplicate elements off a multi-dimension array

I have an array contain this data
Array
(
[id] => Array
(
[0] => 1
[1] => 10
[2] => 4
)
[age] => Array
(
[0] => 1
[1] => 1
[2] => 2
)
)
Now I want to remove duplicates from the ['age'] and leave the first one in tact.
So this would return
Array
(
[id] => Array
(
[0] => 1
[2] => 4
)
[age] => Array
(
[0] => 1
[2] => 2
)
)
Any ideas? Or is there a function already in place to do this?
Like Gordon said, you'd need a custom function to make the relationship but you can use http://php.net/manual/en/function.array-unique.php ?
Wouldn't it be better to have the keys of the age array the corresponding values of the id array?
<?php
$array = array(
'id' => array(0 => 1, 1 => 10, 3 => 4),
'age' => array(0 => 1, 1 => 1, 2 => 2)
);
array_walk($array, 'dupe_killer');
print_r($array);
function dupe_killer(&$value, $key)
{
$value = array_unique($value);
}
?>
You could try this
$array = array('id' => array(1,10,4), 'age'=>array(1,1,2));
$age_array = array();
foreach ($array['age'] as $key => $val) {
if (in_array($val, $age_array))
unset($array['id'][$key], $array['age'][$key]);
$age_array[] = $val;
}
print_r($array);
this returns Array ( [id] => Array ( [0] => 1 [2] => 4 ) [age] => Array ( [0] => 1 [2] => 2 ) )
Regards
Luke

Categories