Help us understand the problem. What is going on with this article?

phpで二次元配列から普通の配列に直す方法!

More than 1 year has passed since last update.

もともとはこんな配列でした。
(本当はもっと多かったけど)

array (size=4)
  0 => 
    array (size=2)
      'key' => string 'firstName' (length=9)
      'value' => string 'yamada' (length=6)
  1 => 
    array (size=2)
      'key' => string 'lastName' (length=8)
      'value' => string 'taro' (length=4)
  2 => 
    array (size=2)
      'key' => string 'gender' (length=6)
      'value' => string 'male' (length=4)
  3 => 
    array (size=2)
      'key' => string 'age' (length=3)
      'value' => int 4
$keyValue = array_map ( function ($data) {
                    return array($data["key"] => $data["value"]);
                }, $array, $array);

まず、これをやると配列はこうなりました。

array (size=9)
  0 => 
    array (size=1)
      'firstName' => string 'yamada' (length=6)
  1 => 
    array (size=1)
      'lastName' => string 'taro' (length=4)
  2 => 
    array (size=1)
      'gender' => string 'male' (length=4)
  3 => 
    array (size=1)
      'age' => int 4

そして次にこれをしました。

$settings = array_reduce($settingsKeyValue, 'array_merge', array());

すると・・・

array (size=9)
  'firstName' => string 'yamada' (length=6)
  'lastName' => string 'taro' (length=4)
  'gender' => string 'male' (length=4)
  'age' => int 4

できたぜ!!!!!

補足

コメントしてくださったやり方がめちゃくちゃ簡単でした・・・。

詳しくはコメントをご覧ください。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away