2014-10-02 88 views
1

我有以下陣列:如何合併使用子陣列創建數組的數組?

最小值:

[2006] => 117 
[2007] => 117 
[2008] => 117 
[2009] => 117 
[2010] => 117 
[2011] => 117 
[2012] => 118 
[2013] => 132 

平均:

[2006] => 170 
[2007] => 174 
... 
[2013] => 166 

最高值:

[2006] => 291 
[2007] => 630 
... 
[2013] => 246 

如何合併他們,所以我有以下陣列與子陣列:

[2006] => array (117, 170, 291), 
[2007] => array (117, 174, 630), 
... 
[2013] => array (132, 166, 246) 

沒有子陣列的解決方案也是一樣。類似的東西也可以解決這個問題:

[2006] => [117, 170, 291] 
[2007] => [117, 174, 630] 
... 
[2013] => [132, 166, 246] 

非常感謝提前!

+0

foreach循環,使用鍵 – 2014-10-02 22:25:46

回答

1

作出有關數組名的假設:

$new=array(); 

foreach($min as $k=>$v){ 
$new[$k]=array($v,$average[$k],$max[$k]); 
} 

並沒有真正理解什麼是「解決方案,而子陣列」的意思 - 你想要一個字符串或?

編輯字符串創建

$new=''; 

foreach($min as $k=>$v){ 
$new.='['.$v.','.$average[$k].','.$max[$k].'],'; 
} 

$new=rtrim($new,',');//remove last comma 
+0

呀,腦子裏想的字符串,因爲最終我真正需要的是一個字符串就像[401170118],[356181120] ...這一切都谷歌圖表: - 你有什麼想法? – 2014-10-02 22:41:51

+0

@AloysiadeArgenteuil更新爲字符串創建 – 2014-10-02 22:45:37

+0

@AloysiadeArgenteuil爲我工作,在這裏演示:http://codepad.viper-7.com/YLmTWR – 2014-10-02 22:59:15

0

它很簡單,只寫了foreach循環,在循環遍歷數組中的一個,我這裏使用的$分鐘陣列。

然後從每個其他數組中獲取等價數據,並將它們全部添加到新數組中,並使用鍵作爲新數組的鍵。

$mins = array(2006 => 117, 2007 => 117, 2008 => 117, 
       2009 => 117, 2010 => 117, 2011 => 117, 
       2012 => 118, 2013 => 132); 

$avgs = array(2006 => 170, 2007 => 174, 2008 => 169, 
       2009 => 179, 2010 => 180, 2011 => 181, 
       2012 => 182, 2013 => 183); 

$maxs = array(2006 => 217, 2007 => 233, 2008 => 322, 
       2009 => 215, 2010 => 216, 2011 => 217, 
       2012 => 231, 2013 => 232); 

$newArray = array(); 

foreach($mins as $year => $min) { 
    $newArray[$year] = array($min, $avgs[$year], $maxs[$year]); 
} 

print_r($newArray); 

輸出是: -

Array 
(
    [2006] => Array 
     (
      [0] => 117 
      [1] => 170 
      [2] => 217 
     ) 

    [2007] => Array 
     (
      [0] => 117 
      [1] => 174 
      [2] => 233 
     ) 

    [2008] => Array 
     (
      [0] => 117 
      [1] => 169 
      [2] => 322 
     ) 

    [2009] => Array 
     (
      [0] => 117 
      [1] => 179 
      [2] => 215 
     ) 

    [2010] => Array 
     (
      [0] => 117 
      [1] => 180 
      [2] => 216 
     ) 

    [2011] => Array 
     (
      [0] => 117 
      [1] => 181 
      [2] => 217 
     ) 

    [2012] => Array 
     (
      [0] => 118 
      [1] => 182 
      [2] => 231 
     ) 

    [2013] => Array 
     (
      [0] => 132 
      [1] => 183 
      [2] => 232 
     ) 

) 

並使用新的數組語法這將是

$mins = [2006 => 117, 2007 => 117, 2008 => 117, 
       2009 => 117, 2010 => 117, 2011 => 117, 
       2012 => 118, 2013 => 132]; 

$avgs = [2006 => 170, 2007 => 174, 2008 => 169, 
       2009 => 179, 2010 => 180, 2011 => 181, 
       2012 => 182, 2013 => 183]; 

$maxs = [2006 => 217, 2007 => 233, 2008 => 322, 
       2009 => 215, 2010 => 216, 2011 => 217, 
       2012 => 231, 2013 => 232]; 

$newArray = []; 

foreach($mins as $year => $min) { 
    $newArray[$year] = [ $min, $avgs[$year], $maxs[$year] ]; 
} 

print_r($newArray); 

如果你想要的結果作爲字符串數組只是改變它像這樣

foreach($mins as $year => $min) { 
    $newArray[$year] = sprintf('[%d,%d,%d]', $min, $avgs[$year], $maxs[$year]); 
} 

print_r($newArray); 

給這個導致

Array 
(
    [2006] => [117,170,217] 
    [2007] => [117,174,233] 
    [2008] => [117,169,322] 
    [2009] => [117,179,215] 
    [2010] => [117,180,216] 
    [2011] => [117,181,217] 
    [2012] => [118,182,231] 
    [2013] => [132,183,232] 
)