如何将数组与子元素

时间:2016-06-04 09:48:37

标签: php

我有一个具有相同customerid的数组。我想将所有相同的customerid数组合并到一个只有几个修正数组的数组中。

Array
(
    [0] => Array
        (
            [customerid] => 13
            [customer_fullname] => Chris
            [profession_id] => 8
            [profession_name] => Producer
         )

    [1] => Array
        (
            [customerid] => 1
            [customer_fullname] => John
            [profession_id] => 8
            [profession_name] => Producer

        )

    [2] => Array
        (
            [customerid] => 13
            [customer_fullname] => Chris
            [profession_id] => 7
            [profession_name] => Camera

        )

)

所以现在我想要像这样创建一个新数组:

Array(
    [customerid] => 13
    [customer_fullname] => Chris
    [new_array] => array(
        [0]=>[profession_id] => 8, [profession_name] => Producer, 
        [1]=>[profession_id] => 7, [profession_name] => Camera
    )
)

花了一些时间但却无法正确使用

3 个答案:

答案 0 :(得分:2)

如果您要合并大量记录,有更好的方法,但如果您想要一种方法来合并两条记录,我只是这样做:

$array1 = array(
      'customerid' => 13
      'customer_fullname' => 'John',
      'profession_id' => 8,
      'profession_name' => 'Producer'
);
$array2 = array(
      'customerid' => 13
      'customer_fullname' => 'John',
      'profession_id' => 7,
      'profession_name' => 'Director'
);

function merge_customers($customerA, $customerB)
{
    $newCustomer = array();
    if ($customerA['customerid'] == $customerB['customerid'])
    {
        $newCustomer['customerid'] = $customerA['customerid'];
        $newCustomer['customer_fullname'] = $customerA['customer_fullname'];

        $newCustomer['new_array'] = array(
            array(
                'profession_id' => $customerA['profession_id'],
                'profession_name' => $customerA['profession_name']
            ),
            array(
                'profession_id' => $customerB['profession_id'],
                'profession_name' => $customerB['profession_name']
            )
        );

        return $newCustomer;
    }

    /* We can't merge these if they're different customers. */
    return NULL;
}

答案 1 :(得分:2)

扩展的解决方案也非常适合发现和合并"多组条目具有相同的customerid。使用过的函数:array_filterarray_count_valuesarray_keysarray_walkarray_chunkarray_values

// supposing $arr is your initial array

// finds which 'customerid' has multiple entries
$dupIds = array_filter(array_count_values(array_column($arr, "customerid")), function($v) {
    return $v > 1;
});

$dupIds = array_keys($dupIds);
$result = [];

array_walk($arr, function($v) use(&$result, $dupIds) {
    if (in_array($v['customerid'], $dupIds)) {
        $parts = array_chunk($v, 2, true);
        if (!isset($result[$v['customerid']])) {
            $result[$v['customerid']] = $parts[0] + ['new_array' => [$parts[1]]];
        } else {
            $result[$v['customerid']]['new_array'][] = $parts[1];
        }
    }
});

print_r(array_values($result));

输出:

Array
(
    [0] => Array
        (
            [customerid] => 13
            [customer_fullname] => Chris
            [new_array] => Array
                (
                    [0] => Array
                        (
                            [profession_id] => 8
                            [profession_name] => Producer
                        )

                    [1] => Array
                        (
                            [profession_id] => 7
                            [profession_name] => Camera
                        )
                )
        )
)

答案 2 :(得分:1)

快速破解,也许有更好的解决方案。 注意:第二个"为每个"只有在阵列可能没有相同字段的情况下才需要循环。

function merge($array1, $array2){
     $result = array();
     foreach($array1 as $key => $value){
         if(isset($array2[$key]) && $array2[$key]!=$array1[$key]){
              $result[$key][]=$value;
              $result[$key][]=$array2[$key];
         }else{
              $result[$key]=$value;
         }
     }
     foreach($array2 as $key => $value){
        if(!isset($result[$key])){
             $result[$key] = $value;
        }
     }
     return $result;
}

print_r(merge($array1, $array2));