使用php array_column函数进行多维数组转换

时间:2022-08-13 08:56:43

I am working on a multi-dimensional array. This is my original array:

我正在研究一个多维数组。这是我原来的数组:

$oriArr = array(
   2 => array( 
      "A" => 'Mapping item1', 
      "B" => array(1 => 'product1', 2 => 'product2', 3 => 'product3'), 
      "C"=>array(1 => 'item1', 2 => 'item2', 3 => 'item3') 
    ), 
   3 => array( 
      "A" => 'Mapping item2', 
      "B" => array(1 => 'product4', 2 => 'product5', 3 => 'product6') 
      "C"=>array(1 => 'item4', 2 => 'item5', 3 => 'item6') 
    ) 
 );

What I am trying to do is to converting the original array into the array like this:

我想要做的是将原始数组转换为数组,如下所示:

$resArr = array(
   "Mapping item1"=>array(
      [1]=>array("product1","item1"),
      [2]=>array("product2","item2"),
      [3]=>array("product3","item3"), 
   ),
   "Mapping item2"=>array(
      [1]=>array("product4","item4"),
      [2]=>array("product5","item5"),
      [3]=>array("product6","item6"), 
   ),
);

I tried to use array_column() but the function only allows array with 2 columns, my original array has 3 columns A,B,C. Any suggestions?

我试图使用array_column()但该函数只允许有2列的数组,我的原始数组有3列A,B,C。有什么建议么?

2 个解决方案

#1


1  

Try the following function.

尝试以下功能。

function my_merge_array( $arr = [] ){
    $result = [];
    foreach ( $arr as $_arr ) {
        $A = $_arr['A'];
        foreach ($_arr['B'] as $key => $value) {
            $result[$A][$key] = [ $value, $_arr['C'][$key] ];
        }
    }
    return $result;
}

print_r( my_merge_array( $oriArr ) );

#2


1  

I wasn't able to do it entirely with array_column, but this works ... as long as the product and item arrays have the same number of elements.

我无法完全使用array_column来完成它,但是只要产品和项目数组具有相同数量的元素,它就可以工作。

<?php
$oriArr = array(
   2 => array(
      'A' => 'Mapping item1',
      'B' => array(1 => 'product1', 2 => 'product2', 3 => 'product3'),
      'C'=>array(1 => 'item1', 2 => 'item2', 3 => 'item3')
    ),
   3 => array(
      'A' => 'Mapping item2',
      'B' => array(1 => 'product4', 2 => 'product5', 3 => 'product6'),
      'C'=>array(1 => 'item4', 2 => 'item5', 3 => 'item6')
    )
 );

$keys = array_column($oriArr,'A');
$products = array_column($oriArr,'B','A');
$items = array_column($oriArr,'C','A');

$result = [];
foreach ($keys as $k) {
        $result[$k] = [];
        if (isset($products[$k])) {
                $i = 1;
                foreach ($products[$k] as $p) {
                        $result[$k][] = [$products[$k][$i],$items[$k][$i]];
                        $i++;
                }
        }
}

var_dump($keys,$products,$items);
var_dump($result);

#1


1  

Try the following function.

尝试以下功能。

function my_merge_array( $arr = [] ){
    $result = [];
    foreach ( $arr as $_arr ) {
        $A = $_arr['A'];
        foreach ($_arr['B'] as $key => $value) {
            $result[$A][$key] = [ $value, $_arr['C'][$key] ];
        }
    }
    return $result;
}

print_r( my_merge_array( $oriArr ) );

#2


1  

I wasn't able to do it entirely with array_column, but this works ... as long as the product and item arrays have the same number of elements.

我无法完全使用array_column来完成它,但是只要产品和项目数组具有相同数量的元素,它就可以工作。

<?php
$oriArr = array(
   2 => array(
      'A' => 'Mapping item1',
      'B' => array(1 => 'product1', 2 => 'product2', 3 => 'product3'),
      'C'=>array(1 => 'item1', 2 => 'item2', 3 => 'item3')
    ),
   3 => array(
      'A' => 'Mapping item2',
      'B' => array(1 => 'product4', 2 => 'product5', 3 => 'product6'),
      'C'=>array(1 => 'item4', 2 => 'item5', 3 => 'item6')
    )
 );

$keys = array_column($oriArr,'A');
$products = array_column($oriArr,'B','A');
$items = array_column($oriArr,'C','A');

$result = [];
foreach ($keys as $k) {
        $result[$k] = [];
        if (isset($products[$k])) {
                $i = 1;
                foreach ($products[$k] as $p) {
                        $result[$k][] = [$products[$k][$i],$items[$k][$i]];
                        $i++;
                }
        }
}

var_dump($keys,$products,$items);
var_dump($result);