与PHP的复杂合并功能

通过为多维数组创建合并函数,我一直在苦苦挣扎.这个场景与单词的详细说明略有不同,我试着用实际的例子来解释它.

$actual_array = [

     'assets' => [1, 2, 3],
     'liabilities' => [1, 2, 3, 4, 5, 6],
     'equity' => [1],
     'income' => [1, 2, 3, 4],
     'expenses' => [1, 2, 3]
];

$merge = [
            'balance_sheet' => ['assets', 'liabilities', 'equity'],
            'income' => ['income', 'expenses'],
         ];

self::merge( $merge, $actual_array );

将合并列的函数

public static function merge( array $merges, array $data )
{
    $bigger_array_length = 0;
    $arr = [];
    $columns = [];

    foreach ($merges as $key => $merge) {

        foreach ($merge as $index => $column) {

           //check which array has bigger length in the iteration
            if($bigger_array_length < count($data[$column]))
                $bigger_array_length = count($data[$column]);

            $columns[] = $column;

        }
        //when i die and dump the columns array on its first iterate
        dd($columns); // assets, liabilities, equity
        if($bigger_array_length> 0) {

            $total_columns = count($columns);// on first iteration 3 assets, liabilities and equity
            for($i = 0; $i < $bigger_array_length; $i++) {
                //
                $arr[$key][] = [ 
                        $columns[$total_columns - 1] => isset($data[ $columns[$total_columns - 1] ][$i]) ? $data[ $columns[$total_columns - 1] ][$i] : 0,
                        $columns[$total_columns - 2] => isset($data[ $columns[$total_columns - 2] ][$i]) ? $data[ $columns[$total_columns - 2] ][$i] : 0,
                        $columns[$total_columns - 3] => isset($data[ $columns[$total_columns - 3] ][$i]) ? $data[ $columns[$total_columns - 3] ][$i] : 0 
                    ]; 

            }


        }
      //when i die and dump on first iteration then it's just fine
    array:1 [▼
  "balance_sheet" => array:6 [▼
    0 => array:3 [▼
      "equity" => 1
      "liabilities" => 1
      "assets" => 1
    ]
    1 => array:3 [▼
      "equity" => 0
      "liabilities" => 2
      "assets" => 2
    ]
    2 => array:3 [▼
      "equity" => 0
      "liabilities" => 3
      "assets" => 3
    ]
    3 => array:3 [▼
      "equity" => 0
      "liabilities" => 4
      "assets" => 0
    ]
    4 => array:3 [▼
      "equity" => 0
      "liabilities" => 5
      "assets" => 0
    ]
    5 => array:3 [▼
      "equity" => 0
      "liabilities" => 6
      "assets" => 0
    ]
  ]
]
$columns = [];

    }

}

我只是被困在这里,无法弄清楚如何让它变得动态.

[$total_columns - 1] // the 1, 2, 3 or so on
//something like [$total_columns - $i]

因此,如果我让foreach进行第二次迭代,那么它会输出未定义的索引错误.

e.x [$total_columns – 3]将在第二次迭代时产生-1索引,因为第二次迭代只有2个总列.

这将导致未定义的偏移量:-1.

我想要实现的目标

"balance_sheet" => array:6 [▼
0 => array:3 [▼
  "equity" => 1
  "liabilities" => 1
  "assets" => 1
]
1 => array:3 [▼
  "equity" => 0
  "liabilities" => 2
  "assets" => 2
]
2 => array:3 [▼
  "equity" => 0
  "liabilities" => 3
  "assets" => 3
]
3 => array:3 [▼
  "equity" => 0
  "liabilities" => 4
  "assets" => 0
]
4 => array:3 [▼
  "equity" => 0
  "liabilities" => 5
  "assets" => 0
]
5 => array:3 [▼
  "equity" => 0
  "liabilities" => 6
  "assets" => 0
]
],
"income" => array:4 [▼
    0 => array:2 [▼
      "expenses" => 1
      "income" => 1
    ]
    1 => array:2 [▼
      "expenses" => 2
      "income" => 2
    ]
    2 => array:2 [▼
      "expenses" => 3
      "income" => 3
    ]
    3 => array:2 [▼
      "expenses" => 0
      "income" => 4
    ]
  ]
]

任何帮助将非常感激.
谢谢

最佳答案

function spec_merge($arr) {
   // find max length of array items
   $m = max(array_map(count, $arr));

   // fill array items to max length by 0
   $a = array_map(function($i) use($m) {
           return ($m - count($i)) ? 
                   $i + array_fill(count($i), $m - count($i), 0) :
                   $i; }, 
        $arr);

   // make array of sourse keys     
   $keys = array_keys($a);

   // transpose array
   array_unshift($a, null);
   $a = call_user_func_array("array_map", $a);

   // set correct keys
   $a = array_map(function ($i) use($keys) {
           return array_combine($keys, $i); }, 
        $a);
   return $a;  
}

$new = spec_merge([
     'assets' => [1, 2, 3],
     'liabilities' => [1, 2, 3, 4, 5, 6],
     'equity' => [1]
     ]);

print_r($new); 

结果

  [[
    [assets] => 1
    [liabilities] => 1
    [equity] => 1
  ][
    [assets] => 2
    [liabilities] => 2
    [equity] => 0
  ][            
    [assets] => 3
    [liabilities] => 3
    [equity] => 0
  ][
    [assets] => 0
    [liabilities] => 4
    [equity] => 0
  ][
    [assets] => 0
    [liabilities] => 5
    [equity] => 0
  ][
    [assets] => 0
    [liabilities] => 6
    [equity] => 0
  ]]

demo

点赞