0

Ok taken the following:

    array(80) {
      ["_edit_lock"]=>
      array(1) {
        [0]=>
        string(12) "1405955175:1"
      }
      ["_edit_last"]=>
      array(1) {
        [0]=>
        string(1) "1"
      }
      ["slide_template"]=>
      array(1) {
        [0]=>
        string(7) "default"
      }
      ["pyre_video"]=>
      array(1) {
        [0]=>
        string(0) ""
      }
      ["pyre_full_width"]=>
      array(1) {
        [0]=>
        string(2) "no"
      }
   }

how do I flatten this to :

array(80) {
  ["_edit_lock"]=>  string(12) "1405955175:1"
  ["_edit_last"]=>  string(1) "1"
  ["slide_template"]=>  string(7) "default"
  ["pyre_video"]=>  string(0) ""
  ["pyre_full_width"]=>   string(2) "no"

}

3 Answers 3

2
<?php
$res = array();

foreach ($src_array as $key => $value) {
  $res[$key] = $value[0];
}

var_dump($res);
?>
Sign up to request clarification or add additional context in comments.

3 Comments

which is quicker? array_map or for each?
For an 80 element array, there would be no noticeable difference.
I was just curious, if it was used on a larger array.
2
$flattened = array_map(function($arr){return $arr[0];}, $your_array);

3 Comments

which is quicker? array_map or for each?
@vimes1984 Why bother such micro level optimization?
@vimes1984 the time required to write the tests to judge which is faster will waste more time than using the faster method will ever save. Hell, I don't think it will even cover writing this comment.
0

Whats about the following?

<?php
$myArray = array(
     'first' => array('blabla'),
     'second' => array('blabla'),
);

foreach($myArray as $key => $layer) {
     if(is_array($layer)) {
          $myArray[$key] = $layer[key($myArray)];
     } else { continue; }
}

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.