2

I have an array:

Array
(
    [0] => Array
        (
            [2010] => 2010
        )

    [1] => Array
        (
            [2011] => 2011
        )

    [2] => Array
        (
            [2012] => 2012
        )

    [3] => Array
        (
            [2013] => 2013
        )

    [4] => Array
        (
            [2014] => 2014
        )

    [5] => Array
        (
            [2015] => 2015
        )

    [6] => Array
        (
            [2016] => 2016
        )

)

Which I want to convert into

array(
[2010] => 2010
[2011] => 2011
[2012] => 2012
)

I have written an function like this:

foreach($arr as $a)
{
   foreach($a as $k=>$sub)
   {
      $newArr[$k] = $sub;
   }
}

Am I doing it right?

4
  • There is nothing wrong in your code. Commented Aug 10, 2016 at 7:48
  • Am I doing it right? -- if(solution is working) { yes } else { no }; Commented Aug 10, 2016 at 7:48
  • Does your mean by right is in performance issues? Commented Aug 10, 2016 at 7:56
  • you've used a redundant foreach loop, one loop would be enough Commented Aug 10, 2016 at 8:25

3 Answers 3

1

Keep in mind What's the difference between array_merge and array + array?

Using the += on an "endresult" array will add the given array into the current array. There are some caveats as explained in the link above to be mindful of in case of overlapping keys.

$arr = [[2012 => 'foo'],[2014 => 'bar'],[2015=>'baz']];
$new = [];
foreach($arr as $i) {
  $new += $i;
}
echo "<PRE>";
var_dump($new);

Live sampe: https://ideone.com/olydNq

Sign up to request clarification or add additional context in comments.

Comments

0

Please try this

$array = Array
(
    '0' => Array
        (
            '2010' => 2010,
        ),

    '1' => Array
        (
            '2011' => 2011,
        ),

    '2' => Array
        (
            '2012' => 2012,
        ),

    '3' => Array
        (
            '2013' => 2013,
        ),

    '4' => Array
        (
            '2014' => 2014,
        ),

    '5' => Array
        (
            '2015' => 2015,
        ),

    '6' => Array
        (
            '2016' => 2016,
        ),

);

$oneDimensionalArray = call_user_func_array('array_merge', $array);
foreach($oneDimensionalArray as $_oneDimensionalArray)
{
     $newArr[$_oneDimensionalArray] = $_oneDimensionalArray;
}   
print_r($newArr)

Comments

0

Please check this code, it works in unformulated array structure also

<?php
$array = array
(
    'aaaaa',
    10,
    '0' => array
        (
            '2010' => 2010,
        ),

    '1' => array
        (
            '2011' => 2011,
        ),

    '2' => array
        (
            '2012' => 2012,
        ),

    '3' => array
        (
            '2013' => 2013,
        ),

    '4' => array
        (
            '2014' => 2014,
        ),

    '5' => array
        (
            '2015' => 2015,
        ),

    '6' => array
        (
            '2016' => 2016,
            '2017' => 2017,
            'xxxx' => 'yyyy',
        ),

);



$newArr=array();



function walk_first($val, $key)
{
    if(is_array($val))
    {
    array_walk($val, 'walk_second');
    }
}
function walk_second($val, $key)
{
    global $newArr;
    $newArr[$key]=$val;
}
array_walk($array, 'walk_first');

print_r($newArr);
?>

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.