php多维混合数组转一维数组的函数
php语言本身没有将多维数组转为一维数组的函数,但是我们可以自己写一个php函数来实现将多维转一维的功能
CODE
public function main($multi)
{
$multi = array(
array(
array('aaaa', 'bbbb'),
'cccc',
),
'dddd',
array(
array('eeee', 'ffff')
),
'!!!!'
);
print_r($this->multiArrayToOne($multi))
}
public function multiArrayToOne($multi)
{
$arr = array();
foreach ($multi as $key => $val) {
if (is_array($val)) {
$arr = array_merge($arr, $this->multiArrayToOne($val));
} else {
$arr[] = $val;
}
}
return $arr;
}
RESULT
Array
(
[0] => aaaa
[1] => bbbb
[2] => cccc
[3] => dddd
[4] => eeee
[5] => ffff
[6] => !!!!
)
$newArray = array_reduce($rulesData,function(&$newArray,$v){
$newArray[$v['index_item']] = $v;
return $newArray;
});
网友评论