不同的数组转换为多维数组

时间:2015-10-09 11:25:48

标签: php arrays

我想在单个数组中组合多个数组输出.Below是我这样做的数组。

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++;
}

4 个答案:

答案 0 :(得分:3)

尝试一下,让我知道结果。它应该工作。 我正在考虑将给定的数组作为一个关联数组,其中包含键"里程碑"," date"和"状态"如果我错了,请纠正我。

$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)

答案 1 :(得分: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
    )

)

答案 2 :(得分:1)

array_column (PHP 5> = 5.5.0)可能会有所帮助 -

$keys = array_keys($arr);
// if the number of element increases(to make it more dynamic)
$count = count($arr['milestone']);
$i= 0;
while($i < $count) {
  $new[] = array_column($arr, $i);
  $i++;
}

foreach($new as $k => $n) {
   $new[$k] = array_combine($keys, $n);
}
var_dump($new);

DEMO

答案 3 :(得分: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;
}

希望它能解决你的问题。