2014-04-10 79 views
1

我有一個數組如下:我應該怎麼做才能在php中獲得這樣的數組?

Array 
(
[Sep] => Array 
    (
     [Support Help Desk] => 24.67 
     [Development] => 7.74 
     [Consulting Services] => 4.04 
    ) 

[Oct] => Array 
    (
     [Support Help Desk] => 14.38 
     [Business Activity] => 1.92 
     [Maintenance Tasks] => 1.00 
     [Development] => 2.11 
    ) 

) 

,我想數組是這樣的:

Array 
    (
     [Support Help Desk] => 24.67,14.38 
     [Development] => 7.74,2.11   
     [Consulting Services] => 4.04,0 
     [Business Activity] => 0,1.92 
     [Maintenance Tasks] => 0,1.00 
    )  

我使用PHP和Zend框架。 但我不知道我應該用什麼方法來獲得這樣的數組?

任何人都可以請指導我嗎?

-

在此先感謝。

回答

0

第三次幸運!我原來在這個問題上忽略了一些微妙之處。試試下面的代碼 - 它有點惡作劇,但它應該適合你。我假設您的原始數組被稱爲$data

// first we need to 'normalise' or fill in the blanks in the contents of the sub array 
// get a unique list of all the keys shared - doing it manually here 

$keys = ['Support Help Desk', 'Business Activity', 'Maintenance Tasks', 'Development', 'Consulting Services']; 

// create a default array with $keys, assigning 0 as the value of each 

$default = array_fill_keys($keys, 0); 

// next fill in the blanks... 
// get the diff (missing keys) between the current element and the default array 
// merge the missing key/value pairs 

array_walk($data, function(&$month, $key, $default) { 
    $diff = array_diff_key($default, $month); 
    $month = array_merge($diff, $month); 
}, $default); 


// now the array is normalised 
// flatten the array... where there are duplicate values for a key, and 
// there will be in all cases now including default values 
// a sub array is created 

$merged = call_user_func_array('array_merge_recursive', $data); 

// finally loop over the merged array 
// and implode each array of values into a comma separated list 

foreach ($merged as &$element) { 
    if (is_array($element)) { 
     $element = implode(', ', $element); 
    } 
} 

// done :) 

var_dump($merged); 

產量:

array (size=5) 
    'Business Activity' => string '0, 1.92' (length=7) 
    'Maintenance Tasks' => string '0, 1' (length=4) 
    'Support Help Desk' => string '24.67, 14.38' (length=12) 
    'Development' => string '7.74, 2.11' (length=10) 
    'Consulting Services' => &string '4.04, 0' (length=7) 

希望這有助於:)

編輯

Live example at eval.in

+0

謝謝Darragh ..它的工作原理。我單獨獲取密鑰並使用您的方法。非常感謝... – Mausami

+0

沒問題@Mausami - 很高興幫助! –

0

如果每個月你可能有不同的密鑰,並且您不提前知道它們,您可以使用下面的代碼($ month_array是您的預處理陣列)。

$new_array = array(); 

foreach($month_array as $month => $month_data) { 
    foreach($month_data as $data => $data_value) { 
    $new_array[$data] = array(); 
    } 
} 

foreach($month_array as $month => $month_data) { 
    foreach($new_array as $data => $data_value) { 
    if(isset($month_data[$data])) { 
     $new_array[$data][] = $month_data[$data]; 
    } else { 
     $new_array[$data][] = 0; 
    } 
    } 
} 

foreach ($new_array as &$key) { 
    $key = implode(", ", $key); 
} 
+0

非常感謝您的幫助.. – Mausami

0

比方說,你的數組存儲在$main_arr和結果陣列$result_arr

$result_arr = array(); 

foreach ($main_arr as $month) { 
    foreach ($month as $key => $val) { 
     if (!isset($result_arr[$key])) { 
      $result_arr[$key] = array($val); 
     } else { 
      array_push($result_arr[$key], $val); 
     } 
    } 
} 

foreach ($result_arr as $key => $val) { 
    $result_arr[$key] = implode(', ', $val); 
} 

print_r($result_arr); //Final output. 
+0

格式化沒有完成。因爲我從手機瀏覽器輸入。 – dikesh

+1

爲您添加了一些格式 –