使用键和添加值合并两个多维数组

时间:2017-06-07 07:13:13

标签: php arrays multidimensional-array

我想使用键(product_id)合并两个数组并添加该值(用法)。 数组1

Array
(
    [0] => Array
        (
            [name] => Reschedule A Service 
            [usage] => 1
            [product_id] => 8
        )

    [1] => Array
        (
            [name] => Adding An Image
            [usage] => 1
            [product_id] => 5
        )

    [2] => Array
        (
            [name] => Each Calendar Event
            [usage] => 1
            [product_id] => 14
        )

)

数组2

Array
(
    [0] => Array
        (
            [name] => Adding An Image
            [usage] => 1
            [product_id] => 5
        )

    [1] => Array
        (
            [name] => Schedule A Service
            [usage] => 3
            [product_id] => 11
        )

    [2] => Array
        (
            [name] => Each Calendar Event
            [usage] => 2
            [product_id] => 14
        )

    [3] => Array
        (
            [name] => Sales Performance Dashboard
            [usage] => 2
            [product_id] => 30
        )

    [4] => Array
        (
            [name] => Quote
            [usage] => 1
            [product_id] => 32
        )

)

我需要像这样合并并添加使用价值的输出。

Array
(
    [0] => Array
        (
            [name] => Adding An Image
            [usage] => 2
            [product_id] => 5
        )

    [1] => Array
        (
            [name] => Schedule A Service
            [usage] => 3
            [product_id] => 11
        )

    [2] => Array
        (
            [name] => Each Calendar Event
            [usage] => 3
            [product_id] => 14
        )

    [3] => Array
        (
            [name] => Sales Performance Dashboard
            [usage] => 2
            [product_id] => 30
        )

    [4] => Array
        (
            [name] => Quote
            [usage] => 1
            [product_id] => 32
        )

    [5] => Array
        (
            [name] => Reschedule A Service 
            [usage] => 1
            [product_id] => 8
        )

)

这是我创建数组的代码


    foreach($query->rows as $product){

     $top_products[]=array(
        'name'=>$product['name'],
        'usage'=>$product['pusage'],
        'product_id'=>$product['product_id']
     );

    }


    foreach($query_2->rows as $product){        

         $top_point_products[]=array(
           'name'=>$product['name'],
           'usage'=>$product['p_usage'],
           'product_id'=>$product['product_id']
         );

    }

4 个答案:

答案 0 :(得分:1)

$first =array(

    array(
        "name" => "Reschedule A Service",
        "usage" => 1,
        "product_id" => 8
    ),
    array(
        "name" => "Adding An Image",
        "usage" => 1,
        "product_id" => 5
    ),
    array(
        "name" => "Each Calendar Event",
        "usage" => 1,
        "product_id" => 14
    )
    );
$second =array(


    array(
        "name" => "Adding An Image",
        "usage" => 1,
        "product_id" => 5
    ),
    array(
        "name" => "Schedule A Service",
        "usage" => 3,
        "product_id" => 11
    ),
    array(
        "name" => "Each Calendar Event",
        "usage" => 2,
        "product_id" => 14
    ),
    array(
        "name" => "Sales Performance Dashboard",
        "usage" => 2,
        "product_id" => 30
    ),
    array(
        "name" => "Quote",
        "usage" => 1,
        "product_id" => 32
    )
);

$result = array_unique(array_merge($first,$second), SORT_REGULAR);
  

使用array_unique& array_merge

答案 1 :(得分:0)

使用array_merge function,如下所示:

$C = array_merge($A, $B);
print_r($C);

Read manual Array merge

答案 2 :(得分:0)

  

试用此代码

<?php

$array1=array
(
    0 => array(
            'name' => "Reschedule A Service", 
            'usage' => 1,
            'product_id' => 8
        ),

    1 => Array
        (
            'name' => "Adding An Image",
            'usage' => 1,
            'product_id' => 5
        ),

    2 => Array
        (
            'name' => "Each Calendar Event",
            'usage' => 2,
            'product_id' => 14
        )

);

$array2=array
(
    0 => Array
        (
            'name' => "Adding An Image",
            'usage' => 1,
            'product_id' => 5
        ),

    1 => Array
        (
            'name' => "Schedule A Service",
            'usage' => 3,
            'product_id' => 11
        ),

    2 => Array
        (
            'name' => "Each Calendar Event",
            'usage' => 5,
            'product_id' => 14
        ),

    3 => Array
        (
            'name' => "Sales Performance Dashboard",
            'usage' => 2,
            'product_id' => 30
        ),

    4 => Array
        (
            'name' => "Quote",
            'usage' => 1,
            'product_id' => 32
        )

);

$product_id1=array_column($array1, 'product_id');  
$product_id2=array_column($array2, 'product_id');  

$new=array_intersect($product_id1,$product_id2);

foreach ($new as $key => $value) {
    if(in_array($new[$key],$product_id2)){
        $array2[array_search($new[$key],$product_id2)]['usage']+=$array1[$key]['usage'];
      }
}
$new1=array_diff($product_id1,$product_id2);
foreach ($new1 as $key => $value) {
        $array2[]=$array1[$key];
 }

foreach ($array2 as $key => $value) {
    echo "[".$key."]=><br>";
    foreach ($value as $key1 => $value1) {
        echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
        echo "[".$key1."]=>".$value1."<br>";

    }
    echo "<br>";
}
?>

<强>输出

  

[0] =&GT;         [name] =&gt;添加图片         [使用方法] =→2         [PRODUCT_ID] =→5

     

[1] =&GT;         [name] =&gt;安排服务         [使用方法] =→3         [PRODUCT_ID] =&GT; 11

     

[2] =&GT;         [name] =&gt;每个日历活动         [使用方法] =大于7         [PRODUCT_ID] =&GT; 14

     

[3] =&GT;         [name] =&gt;销售业绩仪表板         [使用方法] =→2         [PRODUCT_ID] =→30

     

[4] =&GT;         [名称] =&GT;报价         [使用方法] =→1         [PRODUCT_ID] =&GT; 32

     

[5] =&GT;         [name] =&gt;重新安排服务         [使用方法] =→1         [PRODUCT_ID] =&GT; 8

答案 3 :(得分:0)

使用array_merge和简单的foreach循环检查您的情况并更新usage值。

见下文

$result = array_merge($arrArray1, $arrArray2);

$result2 = array(); 

foreach($result as $key => $value){
        if(array_key_exists($value['product_id'], $result2)){
               $result2[$value['product_id']]['usage'] +=  $value['usage'];
        } else{
            $result2[$value['product_id']] = $value;
        }
}

print_r($result2);

如果要重置结果数组索引,请再次使用array_merge

$result2 = array_merge($result2); 

希望这会有所帮助