2017-08-13 67 views
-2

我有這個原始數組在我的代碼php:如何合併和求和相同鍵值的數組的值?

[ 
     { 
      "source": { 
       "id": 2, 
       "name": "Cairo" 
      }, 
      "destination": [ 
       { 
        "count": 3, 
        "name": "Alex", 
        "price": 2000, 
        "discount": null, 
        "distance": 12, 
        "new": false 
       }, 
       { 
        "count": 4, 
        "name": "Alex", 
        "price": 2000, 
        "discount": null, 
        "distance": 12, 
        "new": false 
       } 
      ] 
     }, 
     { 
      "source": { 
       "id": 1, 
       "name": "Zagazig" 
      }, 
      "destination": [ 
       { 
        "count": 1, 
        "name": "Alex", 
        "price": 200, 
        "discount": null, 
        "distance": 100, 
        "new": false 
       }, 
       { 
        "count": 1, 
        "name": "Cairo", 
        "price": 1000, 
        "discount": null, 
        "distance": 100, 
        "new": false 
       }, 
       { 
        "count": 1, 
        "name": "Cairo", 
        "price": 1000, 
        "discount": null, 
        "distance": 100, 
        "new": false 
       } 
      ] 
     } 
    ] 
在目標數組

,如果像亞歷克斯或開羅每個對象被重複的名字,我想給它一個對象合併,只有一個對象與開羅的名字,以及名稱alex和計算或sume計數鍵的一個對象

我想要這個結果................ ............ ...

 [ 
      { 
       "source": { 
        "id": 2, 
        "name": "Cairo" 
       }, 
       "destination": [ 
        { 
         "count": 7, 
         "name": "Alex", 
         "price": 2000, 
         "discount": null, 
         "distance": 12, 
         "new": false 
        } 
       ] 
      }, 
      { 
       "source": { 
        "id": 1, 
        "name": "Zagazig" 
       }, 
       "destination": [ 
        { 
         "count": 1, 
         "name": "Alex", 
         "price": 200, 
         "discount": null, 
         "distance": 100, 
         "new": false 
        }, 
    { 
        "count": 2, 
        "name": "Cairo", 
        "price": 1000, 
        "discount": null, 
        "distance": 100, 
        "new": false 
       }, 
       ] 
      } 
     ] 
+0

如果您不努力解決您的問題,爲什麼我/我們?爲了讓你開始使用'json_deocde(...,true)',你可以擺脫這些對象,並且可以在整個過程中使用數組語法。之後,你可能需要'foreach'和'if'。 – jh1711

回答

0

如果你只需要在名稱相同時計算總和,那麼你只需要迭代每個數組中的每個目標項,並且當您找到重複的名稱時,只需將計數添加到前一個值。它會給你與你的期望的輸出一個新的數組:

$a = [ 
    array(
     "source" => array(
      "id" => 2, 
      "name" => "Cairo" 
     ), 
     "destination" => [ 
      array(
       "count" => 3, 
       "name" => "Alex", 
       "price" => 2000, 
       "discount" => null, 
       "distance" => 12, 
       "new" => false 
      ), 
      array(
       "count" => 4, 
       "name" => "Alex", 
       "price" => 2000, 
       "discount" => null, 
       "distance" => 12, 
       "new" => false 
      ) 
     ] 
    ), 
    array(
     "source" => array(
      "id" => 1, 
      "name" => "Zagazig" 
     ), 
     "destination" => [ 
      array(
       "count" => 1, 
       "name" => "Alex", 
       "price" => 200, 
       "discount" => null, 
       "distance" => 100, 
       "new" => false 
      ), 
      array(
       "count" => 1, 
       "name" => "Cairo", 
       "price" => 1000, 
       "discount" => null, 
       "distance" => 100, 
       "new" => false 
      ), 
      array(
       "count" => 1, 
       "name" => "Cairo", 
       "price" => 1000, 
       "discount" => null, 
       "distance" => 100, 
       "new" => false 
      ) 
     ] 
    ) 
]; 

$new_array = array(); 
foreach ($a as $e) { 
    $tmp = array(); 
    foreach ($e['destination'] as $d) { 
     if (isset($tmp[$d['name']])) { 
      $tmp[$d['name']]['count'] += $d['count']; 
     } else { 
      $tmp[$d['name']] = $d; 
     } 
    } 
    array_push($new_array, array(
     'source' => $e['source'], 
     'destination' => $tmp 
    )); 
} 
echo json_encode($new_array); 

輸出是:

[ 
    { 
     "source":{ 
      "id":2, 
      "name":"Cairo" 
     }, 
     "destination":{ 
      "Alex":{ 
       "count":7, 
       "name":"Alex", 
       "price":2000, 
       "discount":null, 
       "distance":12, 
       "new":false 
      } 
     } 
    }, 
    { 
     "source":{ 
      "id":1, 
      "name":"Zagazig" 
     }, 
     "destination":{ 
      "Alex":{ 
       "count":1, 
       "name":"Alex", 
       "price":200, 
       "discount":null, 
       "distance":100, 
       "new":false 
      }, 
      "Cairo":{ 
       "count":2, 
       "name":"Cairo", 
       "price":1000, 
       "discount":null, 
       "distance":100, 
       "new":false 
      } 
     } 
    } 
] 
0

我建議劃分任務分解成小部分,你可以更好的瞭解,然後拉昇至全。

我會幫助你完成內部數組,最簡單的任務,並且你會看到從這裏開始的一切將變得更加清晰。

因此,如果你得到這個JSON集合:

$json_easy = '[ 
      { 
       "count": 1, 
       "name": "Alex", 
       "price": 200, 
       "discount": null, 
       "distance": 100, 
       "new": false 
      }, 
      { 
       "count": 1, 
       "name": "Cairo", 
       "price": 1000, 
       "discount": null, 
       "distance": 100, 
       "new": false 
      }, 
      { 
       "count": 1, 
       "name": "Cairo", 
       "price": 1000, 
       "discount": null, 
       "distance": 100, 
       "new": false 
      } 
     ]'; 

可以解碼:

$easy_decode = json_decode($json_easy, true); 

,然後創建一個新的數組:

$new_array = array(); 

foreach ($easy_decode as $value) { 

    if(array_key_exists($value['name'],$new_array)) 
     $value['count'] += $new_array[$value['name']]['count']; 

    $new_array[$value['name']] = $value; 
} 

var_dump($new_array); 

如果你仔細看就會看到你現在可以在你的php代碼中委託你自己的函數,並且可以用你的任何標準對數組中的任何值進行求和喜歡。創建函數之後,您可以使用更多或更少的foreach循環開始在陣列中向上移動。