Home > Software engineering >  How to combine two multi-dimenional arrays into one where arrays are not of same length
How to combine two multi-dimenional arrays into one where arrays are not of same length

Time:09-23

I have 2 multi-dimensional arrays I would like to merge into 1, where they have a key 'formatted_day' in common. The issue is that in either of the arrays, depending on the result of the SQL query, this key may not exist. I have tried array_merge_recursive, array_combine, and several loops but I'm stuck.

var_export example array 1:

  array ( 
  0 => array ( 'Sunday' => array ( 'sales' => 0.34, ), ), 
  1 => array ( 'Monday' => array ( 'sales' => 0.1, ), ), 
  2 => array ( 'Wednesday' => array ( 'sales' => 0.1, ), ), 
  3 => array ( 'Friday' => array ( 'sales' => 0.09, ), ), 
  4 => array ( 'Saturday' => array ( 'sales' => 0.37, ), ), 
)

var_export example array 2:

array ( 
  0 => array ( 'Sunday' => array ( 'budget' => 0.26, ), ), 
  1 => array ( 'Monday' => array ( 'budget' => 0.11, ), ), 
  2 => array ( 'Tuesday' => array ( 'budget' => 0.0, ), ), 
  3 => array ( 'Wednesday' => array ( 'budget' => 0.14, ), ), 
  4 => array ( 'Thursday' => array ( 'budget' => 0.0, ), ), 
  5 => array ( 'Friday' => array ( 'budget' => 0.17, ), ), 
  6 => array ( 'Saturday' => array ( 'budget' => 0.32, ), ), 
)

Is there a way to create an array with the following format from this, where in the missing Tuesday in the sales example is added as a 0 also?

array ( 
  0 => array ( 'Sunday' => array ( 'budget' => 0.26, 'sales' => 0.34), ), 
  1 => array ( 'Monday' => array ( 'budget' => 0.11, 'sales' => 0.1), ), 
  2 => array ( 'Tuesday' => array ( 'budget' => 0.0, 'sales' => 0), ), 
  3 => array ( 'Wednesday' => array ( 'budget' => 0.14, 'sales' => 0.1), ), 
  4 => array ( 'Thursday' => array ( 'budget' => 0.0, 'sales' => 0), ), 
  5 => array ( 'Friday' => array ( 'budget' => 0.17, 'sales' => 0.09), ), 
  6 => array ( 'Saturday' => array ( 'budget' => 0.32, 'sales' => 0.37), ), 
)

I have tried the following code

$combined = array_merge_recursive($salesPerDay2, $budgetPerDay);

$keys = array_keys($combined);
for($i = 0; $i < count($combined); $i  ) {
    echo $keys[$i] . "{<br>";
    foreach($combined[$keys[$i]] as $key => $value) {
        echo $key . " : " . $value . "<br>";
    }
    echo "}<br>";
}

But that creates one list:

0{
formatted_date : Sunday
dailySales : 0.34
}
1{
formatted_date : Monday
dailySales : 0.1
}
2{
formatted_date : Wednesday
dailySales : 0.1
}
3{
formatted_date : Friday
dailySales : 0.09
}
4{
formatted_date : Saturday
dailySales : 0.37
}
5{
formatted_date : Sunday
dailyBudget : 0.26
}
6{
formatted_date : Monday
dailyBudget : 0.11
}

And I have tried:

$combined = array_combine($salesPerDay2, $budgetPerDay);
var_dump($combined);

But that gived the following error

Fatal error: Uncaught ValueError: array_combine(): Argument #1 ($keys) and argument #2 ($values) must have the same number of elements in C:\xampp\htdocs\test.php on line 287

CodePudding user response:

Would something like this suit you

$arr1 = [['Sunday'=>['sales'=>0.34]],
        ['Monday'=>['sales'=>0.1]],
        ['Wednesday'=>['sales'=>0.34]]
];
$arr2 = [['Sunday'=>['budget'=>0.34]],
        ['Monday'=>['budget'=>0.1]],
        ['Tuesday'=>['budget'=>0.34]]
];

$new = [];

foreach( $arr1 as $a) {
    foreach ($a as $day => $b) {
        foreach( $b as $key=>$val){
            $new[$day][$key] = $val;
        }
    }
}
foreach( $arr2 as $a) {
    foreach ($a as $day => $b) {
        foreach( $b as $key=>$val){
            $new[$day][$key] = $val;
        }
    }
}

print_r($new);

RESULT

Array
(
    [Sunday] => Array
        (
            [sales] => 0.34
            [budget] => 0.34
        )

    [Monday] => Array
        (
            [sales] => 0.1
            [budget] => 0.1
        )

    [Wednesday] => Array
        (
            [sales] => 0.34
        )

    [Tuesday] => Array
        (
            [budget] => 0.34
        )
)
  • Related