1

I want to combine multiple arrays output in single array. Below is the array I am getting when I do this.

print_r($getData_milestone);

I have arrays as below:

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

And I want to get output such as below:

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
        )

)

I have tried by this code

foreach($getData_milestone['milestone'] as $miledata)
 {
  $allDatamile[$i]=$getData_milestone;
  $allDatamile[$i]=$getData_milestone['date'];
  $allDatamile[$i]=$getData_milestone['status'];
 $i++;
}
1
  • Questions requesting help or code, without any research efforts whatsoever shall not be upvoted until it's editing. Commented Oct 9, 2015 at 11:31

4 Answers 4

3

Try this out, and let me know the result. It should work. I am considering the given array as an associative array with keys "milestone", "date" and "status" .. Correct me if I'm wrong.

$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)
Sign up to request clarification or add additional context in comments.

1 Comment

Welcome. Please accept the answer, so that it would be helpful to others as well.
2

try this,

    $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];
    }

output is

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
    )

)

Comments

1

array_column (PHP 5 >= 5.5.0) might help -

$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

Comments

1

Try it out the bellow code

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

Hope it will solve your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.