php 合并数组中的数据,如果键值相等其值相加

(转载)http://hi.baidu.com/syxrrrr/item/7dfb2387343ce0874414cfdd
/*

* 合并数据,如果键值相等其值相加

* @param array $descs

* @param array $json_wares

*/

public static function getArrayMerge($descs, $json_wares)

{

   if (is_array($descs) && is_array($json_wares)) {

    $arrayMerge = array();

    foreach ($json_wares as $key=>$value) {

     if (array_key_exists($key, $descs)) {

      $arrayMerge[$key] = $value + $descs[$key];

      unset($descs[$key]);

     } else {

      $arrayMerge[$key] = $value;

     }

    }

    return $arrayMerge+$descs;

   } else {

    return false;

   }

} 

 

你可能感兴趣的:(PHP)