2015-10-09 62 views
1

我想結合單個數組中的多個數組輸出。下面是當我這樣做時我得到的數組。不同的數組轉換爲多維數組

print_r($getData_milestone); 

我有數組如下:

[milestone] => Array 
    (
     [0] => milestone 1 
     [1] => milestone 2 
     [2] => milestone 3 
    ) 

[date] => Array 
    (
     [0] => 10/25/2015 
     [1] => 10/30/2015 
     [2] => 11/25/2015 
    ) 

[status] => Array 
    (
     [0] => 1 
     [1] => 1 
     [2] => 0 
    ) 

我想輸出,如下圖所示:

Array 
(
    [0] => Array 
     (
      [milestone] => milestone 1 
      [date] => 10/25/2015 
      [status] => 1 
     ) 

    [1] => Array 
     (
      [milestone] => milestone 2 
      [date] => 10/30/2015 
      [status] => 1 
     ) 

    [2] => Array 
     (
      [milestone] => milestone 3 
      [date] => 11/25/2015 
      [status] => 0 
     ) 

) 

我已經通過這個代碼試圖

foreach($getData_milestone['milestone'] as $miledata) 
{ 
    $allDatamile[$i]=$getData_milestone; 
    $allDatamile[$i]=$getData_milestone['date']; 
    $allDatamile[$i]=$getData_milestone['status']; 
$i++; 
} 
+0

需要幫助或代碼的問題,沒有**任何研究努力**在編輯之前不得提高。 –

回答

3

試試看,並讓我知道結果。它應該工作。 我正在考慮給定的數組作爲關鍵字「里程碑」,「日期」和「狀態」的關聯數組。如果我錯了,糾正我。

$outputArray = array(); 
foreach($givenArray['milestone'] as $key=>$val){ 
    $outputArray[$key]['milestone'] = $val; 
    $outputArray[$key]['date'] = $givenArray['date'][$key]; 
    $outputArray[$key]['status'] = $givenArray['status'][$key]; 
} 

print_r($outputArray) 
+0

歡迎。請接受答案,以便對其他人也有幫助。 –

2

試試這個,

$a["milestone"][] = "milestone 1"; 
    $a["milestone"][] = "milestone 2"; 
    $a["milestone"][] = "milestone 3"; 
    $a["date"][] = "10/25/2015"; 
    $a["date"][] = "10/30/2015"; 
    $a["date"][] = "11/25/2015"; 
    $a["status"][] = "1"; 
    $a["status"][] = "1"; 
    $a["status"][] = "0"; 

    foreach ($a['milestone'] as $key => $val) { 
     $a1[$key]["milestone"] = $val; 
     $a1[$key]["date"] = $a['date'][$key]; 
     $a1[$key]["status"] = $a['status'][$key]; 
    } 

輸出

Array 
(
[0] => Array 
    (
     [milestone] => milestone 1 
     [date] => 10/25/2015 
     [status] => 1 
    ) 

[1] => Array 
    (
     [milestone] => milestone 2 
     [date] => 10/30/2015 
     [status] => 1 
    ) 

[2] => Array 
    (
     [milestone] => milestone 3 
     [date] => 11/25/2015 
     [status] => 0 
    ) 

) 
1

array_column(PHP 5> = 5.5.0)可能會幫助 -

DEMO

1

試試吧波紋代碼

$out= array(); 
$milestone=array 
    (
     "milestone 1", 
     "milestone 2", 
     "milestone 3" 
    ); 

$m_date=array 
    (
     "10/25/2015", 
     "10/25/2015", 
     "10/25/2015" 
    ); 

$status=array 
    (
     0,1,1 
    ); 


for($i=0;$i<count($milestone);$i++){ 
    $comArray=array 
     (
      "milestone" => $milestone[$i], 
      "date" => $m_date[$i], 
      "status" => $status[$i] 
     ) 
    $out[]=$comArray; 
} 

希望這將解決您的問題。