This question already has answers here:
Group array data on one column and sum data from another column
(5 answers)
Closed 1 year ago.
I have read a lot of answers here on SO but havent been able to sort this out.
I have multidimensional array that looks like this:
Array
(
[0] => Array
(
[0] =>
[1] => 655
)
[1] => Array
(
[0] => IT-82
[1] => 14
)
[2] => Array
(
[0] => IT-21
[1] => 5
)
[3] => Array
(
[0] => IT-82
[1] => 7
)
[4] => Array
(
[0] =>
[1] => 3
)
[5] => Array
(
[0] => IT-21
[1] => 4
)
[6] => Array
(
[0] =>
[1] => 3
)
[7] => Array
(
[0] => IT-21
[1] => 3
)
[8] => Array
(
[0] => IT-72
[1] => 7
)
[9] => Array
(
[0] => IT-75
[1] => 22
)
[10] => Array
(
[0] => IT-75
[1] => 3
)
)
I would like to sum the values according to the keys ending with a single array like:
Array
(
=> 661
IT-82 => 21
IT-21 => 12
IT-82 => 12
IT-72 => 7
IT-75 => 25
)
Tried with
foreach ($array as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}
but this only returned the sum of all the values.
Any help appreciated.
Try:
$sumArray = array();
foreach ($array as $k=>$subArray) { //loop through array
if(isset($sumArray[$subArray[0]]))
$sumArray[$subArray[0]] += $subArray[1]; // set 0th index as key and 1st as value and add value to current index
else
$sumArray[$subArray[0]] = $subArray[1];
}
print_r($sumArray);
Output:
Array
(
[] => 661
[IT-82] => 21
[IT-21] => 12
[IT-72] => 7
[IT-75] => 25
)
I suppose it should be:
foreach ($array as $subArray) {
$sumArray[$subArray[0]] += $subArray[1];
}
Related
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 10 months ago.
I have nested array with key & value pair. i want to convert it in single array.
/* This is current array */
Array
(
[id] => Array
(
[0] => 1
[1] => 2
)
[qty] => Array
(
[0] => 1
[1] => 1
)
[price] => Array
(
[0] => 364.41
[1] => 300
)
[amount] => Array
(
[0] => 364.41
[1] => 300
)
)
/*Now, I want this type of array*/
Array
(
[0] => Array
(
[id] => 1
[qty] => 1
[price] => 364.41
[amount] => 364.41
)
[1] => Array
(
[id] => 2
[qty] => 1
[price] => 300
[amount] => 300
)
)
I have tried array_walk and some other solutions but i could not find any proper solution
Thanks in advance
Main array is your nested array and Collection is the result you want
foreach($mainArray["id"] as $key => $value ){
$collection[$key] = ['id' => $value];
foreach(array_keys($mainArray) as $arrayKeysOfmainArray){
if(!array_key_exists($arrayKeysOfmainArray, $collection)){
$collection[$key][$arrayKeysOfmainArray] = $mainArray[$arrayKeysOfmainArray][$key];
}
}
}
print_r($collection);
$mainarray['id'] = Array(1,2,3);
$mainarray['qty'] = Array(1,1,4);
$mainarray['price'] = Array(364.41,300,500);
$mainarray['amount'] = Array(364.41,300,600);
$new = [];
foreach($mainarray as $key=> $singleArray){
if(count($singleArray) > 0){
foreach($singleArray as $childKey=> $value){
$new[$childKey][$key] = $value;
}
};
}`
I have two associative arrays like
Array
(
[0] => Array
(
[0] => 2022-01-19
[1] => 6
)
[1] => Array
(
[0] => 2022-01-20
[1] => 1
)
[2] => Array
(
[0] => 2022-01-21
[1] => 1
)
[3] => Array
(
[0] => 2022-01-22
[1] => 2
)
)
and
Array
(
[0] => Array
(
[0] => 2022-01-17
[1] => 6
)
[1] => Array
(
[0] => 2022-01-18
[1] => 1
)
[2] => Array
(
[0] => 2022-01-21
[1] => 1
)
[3] => Array
(
[0] => 2022-01-23
[1] => 2
)
)
I need to merge them with the date and want a result array-like below
Array
(
[0] => Array
(
[0] => 2022-01-17
[1] => 0
[2] => 6
)
[1] => Array
(
[0] => 2022-01-18
[1] => 0
[2] => 1
)
[2] => Array
(
[0] => 2022-01-19
[1] => 6
[2] => 0
)
[3] => Array
(
[0] => 2022-01-20
[1] => 1
[2] => 0
)
[4] => Array
(
[0] => 2022-01-21
[1] => 1
[2] => 1
)
[5] => Array
(
[0] => 2022-01-22
[1] => 2
[2] => 0
)
[6] => Array
(
[0] => 2022-01-23
[1] => 0
[2] => 2
)
)
I tried with the below code but not any success.
$final_array = [];
foreach($openTicket as $val){
$closeTicketNo = 0;
foreach($closeTicket as $value){
if($val[0] == $value[0]){
$closeTicketNo = $value[1];
}
}
$final_array[] = [$val[0],$val[1],$closeTicketNo];
}
I get all the elements from the $openTicket but not get all the elements from a $closeTicket to my result array $final_array
This code first finds all of the unique dates (using array_unique) from the first values in each array (array_column fetches the values and array_merge puts them into 1 array).
Then it indexes each array by the dates (using array_column again).
Finally looping through the unique dates and adding a new element to the output with the values (using ?? 0 so that if no value is present the array is still filled properly)...
$dates = array_unique(array_merge(array_column($openTicket, 0), array_column($closedTicket, 0)));
$open = array_column($openTicket, 1, 0);
$closed = array_column($closedTicket, 1, 0);
$finalArray = [];
foreach ($dates as $date) {
$finalArray[] = [$date, $open[$date] ?? 0, $closed[$date] ?? 0];
}
You can try like this
$array1 = [
['2022-01-19',6],
['2022-01-20',1],
['2022-01-21',0]
];
$array2 = [
['2022-01-17',6],
['2022-01-20',2],
['2022-01-21',1]
];
function mergeMultiple($array1,$array2){
foreach($array1 as $item1){
$mergedArray[$item1[0]] = $item1;
}
foreach($array2 as $item2){
if(isset($mergedArray[$item2[0]])){
array_push($mergedArray[$item2[0]],$item2[1]);
$mergedArray[$item2[0]] = $mergedArray[$item2[0]];
}else{
$mergedArray[$item2[0]] = $item2;
}
}
return array_values($mergedArray);
}
$mergedArray = mergeMultiple($array1,$array2);
ksort($mergedArray);
print_r($mergedArray);
This question already has answers here:
Push elements from one array into rows of another array (one element per row)
(4 answers)
Closed 5 months ago.
Problem: I want to add the values of Array2 one-after-another to a multidimensional array (array1).
Array 1:
Array
(
[0] => Array
(
[date] => 2020-01-01
[itemsSold] => 25.00000000
)
[1] => Array
(
[date] => 2020-01-02
[itemsSold] => 50.00000000
)
[2] => Array
(
[date] => 2020-01-03
[itemsSold] => 25.00000000
)
)
Array2:
Array
(
[0] => 10
[1] => 15
[2] => 25
)
Goal:
Array
(
[0] => Array
(
[date] => 2020-01-01
[itemsSold] => 25.00000000
[0] => 10
)
[1] => Array
(
[date] => 2020-01-02
[itemsSold] => 50.00000000
[0] => 15
)
[2] => Array
(
[date] => 2020-01-03
[itemsSold] => 25.00000000
[0] => 25
)
)
I am just learning programming since january 2021 so my best solution I got was this:
foreach ($array1 as $main => $value) {
foreach ($array2 as $sec => $entry) {
$array1[$main][] = $array2[$sec];
}
}
print_r($array1);
Output:
Array
(
[0] => Array
(
[date] => 2020-01-01
[itemsSold] => 25.00000000
[0] => 10
[1] => 15
[2] => 25
)
[1] => Array
(
[date] => 2020-01-02
[itemsSold] => 50.00000000
[0] => 10
[1] => 15
[2] => 25
)
[2] => Array
(
[date] => 2020-01-03
[itemsSold] => 25.00000000
[0] => 10
[1] => 15
[2] => 25
)
)
What am I doing wrong in my loop?? How can I get the values of array2 one-by-one into array1??
Thanks for any help ;)
The issue with your code is that you have nested a loop within another when you do not need to. Instead, use the loop format foreach ($array1 as $key => $value) so you can capture its index in $key, then use that as an index to $array2.
foreach ($array1 as $key => $value) {
// Append from $array2 by its index $key
// Using [] will append at index [0]
// You can modify $array1 inside the loop if you
// target it by $key. Notice that we are not using
// $value at all in this loop - only the $key matters.
$array1[$key][] = $array2[$key];
}
print_r($array1);
foreach also allows you to modify the value directly if you use a & reference. This is a little bit shorter:
foreach ($array1 as $key => &$value) {
// Looping over references to $array1 sub-arrays
// means that $value can be modified directly
$value[] = $array2[$key];
}
print_r($array1);
Iteration by reference is described in the PHP foreach manual
I have this array :
Array
(
[0] => Array
(
[0] => Array
(
[0] => 10
[id_list] => 1
[id] => 1
)
[1] => Array
(
[0] => 11
[id_list] => 1
[id] => 1
)
[2] => Array
(
[0] => 12
[id_list] => 1
[id] => 1
)
)
[1] => Array
(
[0] => Array
(
[0] => 11
[id_list] => 2
[id] => 2
)
[1] => Array
(
[0] => 12
[id_list] => 2
[id] => 2
)
)
[2] => Array
(
[0] => Array
(
[0] => 13
[id_list] => 4
[id] => 4
)
)
)
and this code (where $dataListe is the result of a fetchAll query) :
$result = [];
foreach($dataListe as $listeDiff){
$result[] = $listeDiff;
}
// $resultUnique = array_unique($result);
echo "<pre>".print_r($result, true)."</pre>";
as you can see, there's some contact similar in my first and my second array (but contact can be the same in the 1st and the 3rd array, is I choose to add my contact in my 3rd array).
I want to remove the duplicate of each element in the general array.
But when I use array unique, I get this result :
Array
(
[0] => Array
(
[0] => Array
(
[0] => 10
[id_list] => 1
[id] => 1
)
[1] => Array
(
[0] => 11
[id_list] => 1
[id] => 1
)
[2] => Array
(
[0] => 12
[id_list] => 1
[id] => 1
)
)
)
Please I need help to only keep 1 item of each array at the end !
EDIT : I have almost the good result with the code below, but the id 12 is missing
$result = [];
foreach($dataListe as $listeDiff){
foreach($listeDiff as $contact){
if(!in_array($contact,$result)){
$result[] = $contact;
}
break;
}
}
As the PHP docs says :
Note: Note that array_unique() is not intended to work on multi dimensional arrays. (http://php.net/manual/en/function.array-unique.php)
You can try this solution
$uniqueResult = array_map("unserialize", array_unique(
array_map("serialize", $result)
));
as suggested by #daveilers on this question How to remove duplicate values from a multi-dimensional array in PHP.
This question already has answers here:
How to "flatten" a multi-dimensional array to simple one in PHP? [duplicate]
(23 answers)
Closed 5 years ago.
Here is my output: im using more foreach loop with different condition and values put into same array name $returnVal[]=
EX:
foreach($val1 as $vals)
{
$returnVal[]=$vals;
}
foreach($val2 as $vals)
{
$returnVal[]=$vals;
}
foreach($val3 as $vals)
{
$returnVal[]=$vals;
}
foreach($val4 as $vals)
{
$returnVal[]=$vals;
} `
Im getting Output:
Array
(
[0] => Array
(
[0] => 39705--COLUMBUS--LOWNDES--MS
[1] => 39702--COLUMBUS--LOWNDES--MS
[2] => 39710--COLUMBUS--LOWNDES--MS
[3] => 39701--COLUMBUS--LOWNDES--MS
)
[1] => Array
(
[0] => Array
(
[0] => 39705--COLUMBUS--LOWNDES--MS
[1] => 39702--COLUMBUS--LOWNDES--MS
[2] => 39710--COLUMBUS--LOWNDES--MS
[3] => 39701--COLUMBUS--LOWNDES--MS
)
)
[2] => Array
(
[0] => Array
(
[0] => 39705--COLUMBUS--LOWNDES--MS
[1] => 39702--COLUMBUS--LOWNDES--MS
[2] => 39710--COLUMBUS--LOWNDES--MS
[3] => 39701--COLUMBUS--LOWNDES--MS
)
[1] => Array
(
[0] => Array
(
[0] => 39705--COLUMBUS--LOWNDES--MS
[1] => 39702--COLUMBUS--LOWNDES--MS
[2] => 39710--COLUMBUS--LOWNDES--MS
[3] => 39701--COLUMBUS--LOWNDES--MS
)
)
[2] => 57038--JEFFERSON--UNION--SD
)
[3] => 57049--NORTH SIOUX CITY--UNION--SD
)
My Question: remove duplicate values and merge into one level
Push element if it is not exists in the result array, this will avoid duplicate entries. Try the code below,
$returnVal = [];
foreach($val1 as $vals)
{
if (!in_array($vals, $returnVal)) {
$returnVal[]=$vals;
}
} ..etc and so on