我有这样的数组
Array
(
[0] => Array
(
[text] => 1
)
[1] => Array
(
[0] => Array
(
[text] => 2
)
[1] => Array
(
[0] => Array
(
[text] => 3
)
)
[2] => Array
(
[text] => 4
)
)
[2] => Array
(
[text] => 5
)
)
我不知道尺寸的数量,可能有很多尺寸。目标子阵列的关键也可以不是“文本”,而是始终是文本键,而不是数字。如何将此数组转换为这样的数组?
Array
(
[0] => Array
(
[text] => 1
)
[1] => Array
(
[text] => 2
)
[2] => Array
(
[text] => 3
)
[3] => Array
(
[text] => 4
)
[4] => Array
(
[text] => 5
)
)
更新:好的,我没有解释,也没有理解整个问题。问题是数组是通过递归形成的,我有一个函数:
public static function goToAction($action)
{
$actions = array();
$logic = file_get_contents('../../logic/logic.json');
$logic_array = json_decode($logic, true);
unset($logic);
if (!isset($logic_array[$action])) {
return false;
} else {
foreach ($logic_array[$action] as $action) {
$actions[] = self::parseActionType($action);
}
}
return $actions;
}
然后我的数组($ data)在这里形成
public static function parseActionType($actions)
{
$data = array();
foreach ($actions as $key => $action) {
switch ($key) {
case 'goto': {
$goto_actions = self::goToAction($action);
foreach ($goto_actions as $goto_action) {
$data[]= $goto_action;
}
} break;
....
}
}
所以这些函数可以相互调用,并且可以有递归,正如我所知,当这种情况发生时,这段代码$data[] = $goto_action
将所有接收到的动作放在一个元素中,但是我需要放置每个{{1在$goto_action
数组
答案 0 :(得分:3)
您可以使用array_walk_recursive
遍历所有值。的 live demo 强>
$result = [];
array_walk_recursive($array, function($v, $k) use(&$result){
$result[] = [$k => $v];
});