LoginSignup
2
0

More than 5 years have passed since last update.

多次元配列をキーを維持して flatten

Last updated at Posted at 2019-02-01

表題の件です。査収ください。

$d = [
  0=>[
    1=>[
      2=>'a', 
      3=>'b'
    ],
   'a'=>'x'
  ]
];

function flt($values, &$ret, $keys = [], $key = null){
  if($key !== null) $keys[] = $key;
  if(!is_array($values)) $ret[implode('.', $keys)] = $values;
  else foreach($values as $k=>$v){ flt($v, $ret, $keys, $k);}
}

flt($d, $ret);
print_r($ret);
/*
array [
  "0.1.2" => "a"
  "0.1.3" => "b"
  "0.a" => "x"
]
*/

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0