0

I have the following array:

$arr = array(
           array("2014-03-13"=>array("a"=>1,"b"=>2)),
           array("2014-03-12"=>array("a"=>4,"b"=>3))
       );

And I would like the change the way it looks to something more like this.

$arr = array(0=>array("date"=>"2014-03-13","a"=>1,"b"=>2),
             1=>array("date"=>"2014-03-12","a"=>4,"b"=>3));

Heres what I have so far.

$keys = array();
$vals = array();
foreach($arr as $row){
   foreach($row as $key=>$val){
     $keys[]=array("date"=>$key);
     foreach($val as $keys=>$values){
        $vals[]=array($keys=>$values);
     }
   }
}

The array which gets the dates works fine so in the below example the $keys array works however the $vals array does not work as intended and instead gives me an array similar to this.

Array ( [0] => Array ( [a] => 1 ) 
        [1] => Array ( [b] => 2 ) 
        [2] => Array ( [a] => 4 ) 
        [3] => Array ( [b] => 3 ) 
        [4] => Array ( [a] => 4 ) 
        [5] => Array ( [b] => 3 ) ) 

Any help to get the array desired is appreciated.

4
  • In that first array there is a 0=>, but shouldn't there also be a 1=>? Commented Mar 13, 2014 at 21:34
  • @IarsAnders No there shouldn't and really its irrelevant if it wasn't there the array still prints out the same way. Commented Mar 13, 2014 at 21:36
  • 1
    Or, preferably, be no 0=> at all as numeric indexes are implicit in php. Commented Mar 13, 2014 at 21:37
  • Ill remove it sorry. Its just how the array was given to me so I kept it there. Commented Mar 13, 2014 at 21:38

3 Answers 3

1

It should be

$result = array();
foreach($arr as $row) {
    foreach($row as $date=>$values) {
        $values['date'] = $date;
        $result[] = $values;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
$arr = array(
    array(
        "2014-03-13" => array("a"=>1, "b"=>2)
    ),
    array(
        "2014-03-12" => array("a"=>4, "b"=>3)
    ),
);


$result = array();
foreach ($arr as $item) {
    foreach ($item as $date => $values) {
        $result[] = array_merge(array('date' => $date), $values);
    }
}

var_dump($result);

Comments

0
        Here is the code:

   $arr = array(
         0=>array("2014-03-13"=>array("a"=>1,"b"=>2)),
           array("2014-03-12"=>array("a"=>4,"b"=>3))
       );

$finalarray=array();
foreach($arr as $k=>$v){    

    foreach($v as $kk=>$vv){
        $newarray=array();

        $newarray["date"]=$kk;
        foreach($vv as $kkk=>$vvv){
            $newarray[$kkk]=$vvv;

        }


    }

    $finalarray[]=$newarray;
}

echo "<pre>";
print_r($finalarray);
echo "</pre>";

        **Here is the output:**

        Array
        (
            [0] => Array
                (
                    [date] => 2014-03-13
                    [a] => 1
                    [b] => 2
                )

            [1] => Array
                (
                    [date] => 2014-03-12
                    [a] => 4
                    [b] => 3
                )

        )

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.