2015-09-13 140 views
1

我有一個數組$vd_arrPHP合併與和陣列

array (size=3) 
    0 => 
    array (size=3) 
     'delivnote_id' => string '1' (length=1) 
     'qty' => string '76' (length=2) 
     'id' => int 17 
    1 => 
    array (size=3) 
     'delivnote_id' => string '1' (length=1) 
     'qty' => string '87' (length=2) 
     'id' => int 16 
    2 => 
    array (size=3) 
     'delivnote_id' => string '1' (length=1) 
     'qty' => string '889' (length=3) 
     'id' => int 17 

我需要通過ID和數量值的總和進行合併,這樣的結果將是:

array (size=2) 
    0 => 
    array (size=3) 
     'id' => int 17 
     'qty' => int 965 
     'delivnote_id' => string '1' (length=1) 
    1 => 
    array (size=3) 
     'id' => int 16 
     'qty' => int 87 
     'delivnote_id' => string '1' (length=1) 

這個腳本做的工作,但是PHP通知我undefined index: qty

$vendel = array(); 
$indexArray = array(); 
foreach ($vd_arr as $miniarray) { 
    $vendel[$miniarray['id']]['id'] = $miniarray['id']; 
    $vendel[$miniarray['id']]['qty'] += $miniarray['qty']; 
    $vendel[$miniarray['id']]['delivnote_id'] = $miniarray['delivnote_id']; 
} 

foreach($vendel as $value) { 
    $indexArray[] = $value; 
} 

return $indexArray; 

如何避免undefined index從上面的腳本?謝謝。

+0

使用isset()函數就可以避免未定義指數 –

+0

@AnkurTiwari的問題,請你寫出完整的代碼的另一種方法? – ichadhr

+0

是否能夠提供您有警告或通知的行號。 –

回答

2

試試這個

$vendel = array(); 
$indexArray = array(); 
foreach ($vd_arr as $miniarray) { 
    $vendel[$miniarray['id']]['id'] = $miniarray['id']; 

    if(isset($vendel[$miniarray['id']]['qty'])) 
    { 
     $vendel[$miniarray['id']]['qty'] += $miniarray['qty']; 
    } 
    else 
    { 
     $vendel[$miniarray['id']]['qty'] = $miniarray['qty']; 
    } 

    $vendel[$miniarray['id']]['delivnote_id'] = $miniarray['delivnote_id']; 
} 

foreach($vendel as $value) { 
    $indexArray[] = $value; 
} 

return $indexArray; 
+0

Your're welcome @ichadhr –

0

適應代碼,包括檢查陣列性能存在訪問他們面前:

$vendel = array(); 
    $indexArray = array(); 
    foreach ($vd_arr as $miniarray) { 

     // Skips adding this mini array if an ID is note present 
     if(($id = isset($miniarray['id']) ? $miniarray['id'] : null) === null) { 
      continue; 
     } 

     // The default values at the end of these lines can be swapped out for more suitable defaults 
     $qty = isset($miniarray['qty']) ? $miniarray['qty'] : 0; 
     $delivnote_id = isset($miniarray['delivnote_id']) ? $miniarray['delivnote_id'] : null; 

     $vendel[$id]['id'] = $id; 
     $vendel[$id]['qty'] += $qty; 
     $vendel[$id]['delivnote_id'] = $delivnote_id; 
    } 

    foreach($vendel as $value) { 
     $indexArray[] = $value; 
    } 

    return $indexArray; 

這也跳過補充說,沒有一個「身份證」一個$ miniarray項目。

0
  • 根據提供的代碼,問題出現在$ vendel [$ miniarray ['id']] ['qty'] + = $ miniarray ['qty'];
  • 由於您尚未定義$ vendel [$ miniarray ['id']] ['qty']的值,所以此問題出現。
  • 爲了解決這個問題,你需要定義$ vendel [$ miniarray ['id']] ['qty']的初始值。
  • 您需要在您寫入的foreach語句之前將qty的初始值定義爲零。
  • 的foreach($ vd_arr爲$ miniarray){$文德爾[$ miniarray [ 'ID']] [ '數量'] = 0;}
0

如已經簡單地提及檢查數量isset加入之前它:

$vendel = array(); 
$indexArray = array(); 
foreach ($vd_arr as $miniarray) { 

    $vendel[$miniarray['id']]['id'] = $miniarray['id']; 
    $vendel[$miniarray['id']]['delivnote_id'] = $miniarray['delivnote_id']; 
    // the magic is in the next line 
    if(!isset($vendel[$miniarray['id']]['qty'])) 
     $vendel[$miniarray['id']]['qty'] = 0; 
    $vendel[$miniarray['id']]['qty'] += $miniarray['qty']; 
} 

foreach($vendel as $value) { 
    $indexArray[] = $value; 
} 
return $indexArray; 

與一些重構,如果我們使用的「身份證」作爲$ indexArray的關鍵,我們可以擺脫第二個for循環的

$vendel = array(); 
$indexArray = array(); 

foreach ($vd_arr as $miniarray) { 
    if(!isset($indexArray[$miniarray['id']])) { 
     $indexArray[$miniarray['id']] = array(
      'delivnote_id' => $miniarray['delivnote_id'] 
      ,'qty' => 0 
     ); 
    } 
    $indexArray[$miniarray['id']]['qty'] += $miniarray['qty']; 
} 

return $indexArray; 

或者,如果你需要這些指標是數值

$vendel = array(); 
$indexArray = array(); 
foreach ($vd_arr as $miniarray) { 
    if(!isset($indexArray[$miniarray['id']])) { 
     $indexArray[$miniarray['id']] = array(
      'id' => $miniarray['id'] 
      ,'delivnote_id' => $miniarray['delivnote_id'] 
      ,'qty' => 0 
     ); 
    } 
    $indexArray[$miniarray['id']]['qty'] += $miniarray['qty']; 
} 
return array_values($indexArray); 
0

利用泛函編程

<?php 


$data = json_decode('[ 
    { 
     "id": 1, 
     "value1": 5, 
     "value2": 10 
    }, 
    { 
     "id": 2, 
     "value1": 50, 
     "value2": 100 
    }, 
    { 
     "id": 1, 
     "value1": 1, 
     "value2": 2 
    }, 
    { 
     "id": 2, 
     "value1": 15, 
     "value2": 20 
    }, 
    { 
     "id": 3, 
     "value1": 15, 
     "value2": 20 
    } 
]'); 

//print_r(array_group_by($data,'id')); 
print_r(array_sum_by($data,'id',['value1','value2'])); 
//print_r(array_sum(array_column($data,'value1'))); 

function array_sum_by($array,$group,$keys) 
{ 
    $groupeddata = array_group_by($array,$group); 
    return array_map(function($items) use($group,$keys) { 

     foreach($keys as $key) 
     { 
      $values[$key] = array_sum(array_column($items,$key)); 
     } 
     $first = array_shift($items); 
     if(is_object($first) && isset($first->{ $group })){ 
      $values = array_merge([ $group => $first->{ $group } ], $values); 
     }elseif(isset($first[$group])){ 
      $values = array_merge([ $group => $first[$group] ], $values); 
     } 
     return $values; 
    },$groupeddata); 
} 

function array_group_by(array $array, $key) 
    { 
     if (! is_string($key) && ! is_int($key) && ! is_float($key) && ! is_callable($key)) { 
      trigger_error('array_group_by(): The key should be a string, an integer, or a callback', E_USER_ERROR); 
      return null; 
     } 
     $func = (is_callable($key) ? $key : null); 
     $_key = $key; 
     // Load the new array, splitting by the target key 
     $grouped = []; 
     foreach ($array as $value) { 
      if (is_callable($func)) { 
       $key = call_user_func($func, $value); 
      } elseif (is_object($value) && isset($value->{ $_key })) { 
       $key = $value->{ $_key }; 
      } elseif (isset($value[ $_key ])) { 
       $key = $value[ $_key ]; 
      } else { 
       continue; 
      } 
      $grouped[ $key ][] = $value; 
     } 
     // Recursively build a nested grouping if more parameters are supplied 
     // Each grouped array value is grouped according to the next sequential key 
     if (func_num_args() > 2) { 
      $args = func_get_args(); 
      foreach ($grouped as $key => $value) { 
       $params = array_merge([ $value ], array_slice($args, 2, func_num_args())); 
       $grouped[ $key ] = call_user_func_array('array_group_by', $params); 
      } 
     } 
     return $grouped; 
    }