PHP,结合多维数组的值

时间:2015-09-12 08:47:46

标签: php multidimensional-array

我有一个像这样的多维数组:

Array
(
   [0] => Array ( [product_id] => 46 [product_name] => Product1 [id_attribute] => 26 [subscr_value] => 365 )
   [1] => Array ( [product_id] => 46 [product_name] => Product1 [id_attribute] => 74 [subscr_value] => 3 )
   [2] => Array ( [product_id] => 42 [product_name] => Product2 [id_attribute] => 25 [subscr_value] => 90 )
   [3] => Array ( [product_id] => 42 [product_name] => Product2 [id_attribute] => 33 [subscr_value] => 1 )
)

我想这样做:

Array
(
    [0] => Array ( [product_id] => 46 [product_name] => Product1 [subscr_value] => 365 [subscr_value2] => 3 )
    [1] => Array ( [product_id] => 42 [product_name] => Product2 [subscr_value] => 90  [subscr_value2] => 1 )
)

哪种方式最简单?

3 个答案:

答案 0 :(得分:0)

所以,这就是你想要的。

$md_array = [
               array ( 'product_id' => 46, 'product_name' => 'Product1', 'id_attribute' => 26, 'subscr_value' => 365 ),
               array ( 'product_id' => 46, 'product_name' => 'Product1', 'id_attribute' => 74, 'subscr_value' => 3 ),
               array ('product_id' => 42, 'product_name' => 'Product2', 'id_attribute' => 25, 'subscr_value' => 90 ),
               array ('product_id' => 42, 'product_name' => 'Product2', 'id_attribute' => 33, 'subscr_value' => 1 )

            ];

function combine_similar($input = array()){
    $temp = [];
    foreach($input as $vals){

        $id = $vals['product_id'];
        if(isset($temp[$id])){
            $temp[$id]['subscr_value'.(count($temp[$id]) - 2)] = $vals['subscr_value']; 
        }else{
            $temp[$id] = $vals;
        }
    }
    return array_values($temp);
}

//manipulate and print the results
print_r(combine_similar($md_array));

/* outputs */
Array
(
[0] => Array
    (
        [product_id] => 46
        [product_name] => Product1
        [id_attribute] => 26
        [subscr_value] => 365
        [subscr_value2] => 3
    )

[1] => Array
    (
        [product_id] => 42
        [product_name] => Product2
        [id_attribute] => 25
        [subscr_value] => 90
        [subscr_value2] => 1
    )

)

答案 1 :(得分:0)

虽然这不是您所期望的答案,但我建议将id_attributesubscr_value分组到数组中。

<?php

$products = [
    [
        'product_id' => 46,
        'product_name' => 'Product1',
        'id_attribute' => 26,
        'subscr_value' => 365,
    ],
    [
        'product_id' => 46,
        'product_name' => 'Product1',
        'id_attribute' => 74,
        'subscr_value' => 3,
    ],
    [
        'product_id' => 42,
        'product_name' => 'Product2',
        'id_attribute' => 25,
        'subscr_value' => 90,
    ],
    [
        'product_id' => 42,
        'product_name' => 'Product2',
        'id_attribute' => 33,
        'subscr_value' => 1,
    ],
];

$base_keys = ['product_id', 'product_name'];
$other_keys = ['id_attribute', 'subscr_value']; // Delete 'id_attribute' unless you need it

$new_products = [];
foreach ($products as $product) {
    $id = $product[reset($unique_keys)];
    foreach ($base_keys as $key) {
        $new_products[$id][$key] = $product[$key];
    }
    foreach ($other_keys as $key) {
        $new_products[$id][$key][] = $product[$key];
    }
}
$new_products = array_values($new_products);

print_r($new_products);

这将输出:

Array
(
    [0] => Array
        (
            [product_id] => 46
            [product_name] => Product1
            [id_attribute] => Array
                (
                    [0] => 26
                    [1] => 74
                )

            [subscr_value] => Array
                (
                    [0] => 365
                    [1] => 3
                )

        )

    [1] => Array
        (
            [product_id] => 42
            [product_name] => Product2
            [id_attribute] => Array
                (
                    [0] => 25
                    [1] => 33
                )

            [subscr_value] => Array
                (
                    [0] => 90
                    [1] => 1
                )

        )

)

答案 2 :(得分:0)

这样做:

$srcArr = Array
(
   Array ( 'product_id' => 46, 'product_name' => 'Product1', 'id_attribute' => 26, 'subscr_value' => 365 ),
   Array ( 'product_id' => 46, 'product_name' => 'Product1', 'id_attribute' => 74, 'subscr_value' => 3 ),
   Array ( 'product_id' => 46, 'product_name' => 'Product1', 'id_attribute' => 74, 'subscr_value' => 30 ),
   Array ( 'product_id' => 42, 'product_name' => 'Product2', 'id_attribute' => 25, 'subscr_value' => 90 ),
   Array ( 'product_id' => 42, 'product_name' => 'Product2', 'id_attribute' => 33, 'subscr_value' => 1 ),
   Array ( 'product_id' => 42, 'product_name' => 'Product2', 'id_attribute' => 33, 'subscr_value' => 32 ),
   Array ( 'product_id' => 42, 'product_name' => 'Product2', 'id_attribute' => 33, 'subscr_value' => 75 )
);

function combineDuplicate($srcArr){
   foreach($srcArr as $p) {
     $id             = $p['product_id'];
     $tempArr[$id][] = $p;
     $dupCnt         = count($tempArr[$id]);

     $index = ($dupCnt > 1) ? 'subscr_value' . $dupCnt : '';
     ($result[$id]) ? $result[$id][$index] = $p['subscr_value'] : $result[$id] = $p;
   }

   return array_values($result);
}

print_r(combineDuplicate($srcArr));