2016-01-08 61 views
1

的所有值多維數組這是我的數組:PHP - 基於特定的鍵

$arr = array(
    0 => array(
     'title' => 'test1', 
     'count' => 4, 
     'month' => 'jan-2015' 
    ), 
    1 => array(
     'title' => 'test2', 
     'count' => 10, 
     'month' => 'jan-2015' 
    ), 
    2 => array(
     'title' => 'test3', 
     'count' => 14, 
     'month' => 'jun-2015' 
    ), 
    3 => array(
     'title' => 'test4', 
     'count' => 45, 
     'month' => 'july-2015' 
    ), 
    4 => array(
     'title' => 'test1', 
     'count' => 40, 
     'month' => 'jun-2015' 
    ), 
); 

我已經爲低於這個數組轉換:

$arr = array(
    'jan-2015' => array(
     0 => array(
     'title' => 'test1', 
     'count' => 4, 
    ), 
     1 => array(
     'title' => 'test2', 
     'count' => 10, 
    ), 
     2 => array(
     'title' => 'test3', 
     'count' => 0, 
    ), 
     3 => array(
     'title' => 'test4', 
     'count' => 0, 
    ), 
    ), 
    'jun-2015' => array(
     0 => array(
     'title' => 'test1', 
     'count' => 40, 
    ), 
     1 => array(
     'title' => 'test2', 
     'count' => 0, 
    ), 
     2 => array(
     'title' => 'test3', 
     'count' => 14, 
    ), 
     3 => array(
     'title' => 'test4', 
     'count' => 0, 
    ), 
    ), 
    'july-2015' => array(
     0 => array(
     'title' => 'test1', 
     'count' => 0, 
    ), 
     1 => array(
     'title' => 'test2', 
     'count' => 0, 
    ), 
     2 => array(
     'title' => 'test3', 
     'count' => 0, 
    ), 
     3 => array(
     'title' => 'test4', 
     'count' => 45, 
    ), 
    ), 
); 

也就是說,如果有是沒有特定月份的標題,那麼我需要添加標題和計數將是0.如何做到這一點? 我試過這個概念:

$gen_arr = array_fill_keys(array_column($arr, 'month'), array()); 
$final_arr = array(); // Array to store the result 
foreach ($gen_arr as $gen_key => $gen_value) { 
    foreach ($arr as $org_key => $org_value) { 
    //temporarily store the original array 
    $temp = $org_value; 
    if ($gen_key != $temp['month'] && !in_array($temp['title'], $final_arr[$gen_key])) { 
     $temp['month'] = $gen_key; 
     $temp['count'] = 0; 
    } 

    $final_arr[$gen_key][] = $temp; 
    } 
} 
return $final_arr; 

但我無法得到我想要的。有沒有其他解決方案?

+0

你的意思是你想都一年12個月的是陣列的鑰匙? 即使月份不存在於輸入數組中,它應該出現在輸出數組中,如'feb-2015 => array()'? –

+0

不是這樣的...只有在沒有標題的情況下才需要添加標題值'mon-2016' – Guru

+0

標題是動態的嗎?它可以去測試10000 ..? – roullie

回答

0

你可以試試這個代碼:

$arr = array(
    0 => array(
     'title' => 'test1', 
     'count' => 4, 
     'month' => 'jan-2015' 
    ), 
    1 => array(
     'title' => 'test2', 
     'count' => 10, 
     'month' => 'jan-2015' 
    ), 
    2 => array(
     'title' => 'test3', 
     'count' => 14, 
     'month' => 'jun-2015' 
    ), 
    3 => array(
     'title' => 'test4', 
     'count' => 45, 
     'month' => 'july-2015' 
    ), 
    4 => array(
     'title' => 'test1', 
     'count' => 40, 
     'month' => 'jun-2015' 
    ), 
); 

$titles = array_unique(array_column($arr, 'title')); 
$months = array_unique(array_column($arr, 'month')); 

// Construct an array with the correct structure but with zero counts 
$resultingArray = []; 
foreach($months as $month){ 
    if(!is_array($resultingArray[$month])){ 
     $resultingArray[ $month ] = []; 
    } 
    foreach($titles as $title){ 
     $resultingArray[ $month ][] = ['title' => $title, 'count' => 0, 'month' => $month ]; 
    } 
} 
// And now populate the counts 
foreach($arr as $a){ 
    foreach($resultingArray as $month => &$tempArray){ 
     foreach($tempArray as &$array){ 
      if($a['month'] === $month && $a['title'] === $array['title']){ 
       $array['count'] = $a['count']; 
      } 
     } 
    } 
} 

echo '<pre>'; 
print_r($resultingArray); 
echo '</pre>'; 
+0

@Guru輸出和你發佈的一樣。可能你可以更詳細地解釋你到底需要什麼? – mrun

+0

這是正確的答案我想要的。謝謝 – Guru

+0

太棒了!在這種情況下,你可以收回downvote嗎? – mrun

0

請試試這個代碼爲您的期望。

$new_array = array(); 
foreach($arr as $each_arr){ 
    $month = $each_arr['month']; 
    $new_array[$month][] = array(
     'title' => $each_arr['title'], 
     'count' => $each_arr['count'], 
    ); 
} 

var_dump($new_array); 

echo '<pre>'; 
print_r($new_array); 
echo '</pre>'; 
+1

這不會產生所需的數組。請仔細閱讀要求! – mrun

+0

爲什麼我的答案downvote? – mrun