本文介绍了未知深度的多维数组一dimensonal阵列和PHP相关的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个递归函数抽出,看起来像这样的数据
阵列
(
[17] =>排列
(
[CAT_ID] => 17
[cat_name] => test.example.1
[cat_parent] => 16
[cat_slug] =>测试实施例1
)[18] =>排列
(
[16] =>排列
(
[CAT_ID] => 16
[cat_name] => test.example.2
[cat_parent] => 15
[cat_slug] =>试验例2
) [17] =>排列
(
[15] =>排列
(
[CAT_ID] => 15
[cat_name] => test.example.3
[cat_parent] => 6
[cat_slug] =>试验实施例3
) [16] =>排列
(
[6] =>排列
(
[CAT_ID] => 6
[cat_name] => test.example.4
[cat_parent] => 2
[cat_slug] =>试验实施例4
) [7] =>排列
(
[2] =>排列
(
[CAT_ID] => 2
[cat_name] => test.example.5
[cat_parent] => 0
[cat_slug] =>试验例5
) ) ) ) ))
我无法找到一个方法来创建这个数组中的一维的,所以我希望有人能帮助它的一维与一些阵列的发挥,保持相关按键。
例如
阵列
(
[17] =>排列
(
[CAT_ID] => 17
[cat_name] => test.example.1
[cat_parent] => 16
[cat_slug] =>测试实施例1
) [16] =>排列
(
[CAT_ID] => 16
[cat_name] => test.example.2
[cat_parent] => 15
[cat_slug] =>试验例2
)
[15] =>排列
(
[CAT_ID] => 15
[cat_name] => test.example.3
[cat_parent] => 6
[cat_slug] =>试验实施例3
)
[6] =>排列
(
[CAT_ID] => 6
[cat_name] => test.example.4
[cat_parent] => 2
[cat_slug] =>试验实施例4
)
[2] =>排列
(
[CAT_ID] => 2
[cat_name] => test.example.5
[cat_parent] => 0
[cat_slug] =>试验例5
) )
解决方案
功能扁平化($阵列,放大器和; $ newArray){
的foreach($数组作为$ I => $ EL){
如果(!is_array($阵列)){
返回;
}
如果(使用isset($埃尔['CAT_ID'])){
$ newArray [$ i] = $ EL;
}其他(is_array($ EL)){
扁平化($ EL,$ newArray);
}
}
}
$结果=阵列();
扁平化($数组$结果);
I have a recursive function pumping out data that looks like this
Array
(
[17] => Array
(
[cat_id] => 17
[cat_name] => test.example.1
[cat_parent] => 16
[cat_slug] => Test Example 1
)
[18] => Array
(
[16] => Array
(
[cat_id] => 16
[cat_name] => test.example.2
[cat_parent] => 15
[cat_slug] => Test Example 2
)
[17] => Array
(
[15] => Array
(
[cat_id] => 15
[cat_name] => test.example.3
[cat_parent] => 6
[cat_slug] => Test Example 3
)
[16] => Array
(
[6] => Array
(
[cat_id] => 6
[cat_name] => test.example.4
[cat_parent] => 2
[cat_slug] => Test Example 4
)
[7] => Array
(
[2] => Array
(
[cat_id] => 2
[cat_name] => test.example.5
[cat_parent] => 0
[cat_slug] => Test Example 5
)
)
)
)
)
)
I cannot find a way to create this array in one dimensional so I am hoping someone can help make it one dimensional with some array play, keeping the relevant keys.
example.
Array
(
[17] => Array
(
[cat_id] => 17
[cat_name] => test.example.1
[cat_parent] => 16
[cat_slug] => Test Example 1
)
[16] => Array
(
[cat_id] => 16
[cat_name] => test.example.2
[cat_parent] => 15
[cat_slug] => Test Example 2
)
[15] => Array
(
[cat_id] => 15
[cat_name] => test.example.3
[cat_parent] => 6
[cat_slug] => Test Example 3
)
[6] => Array
(
[cat_id] => 6
[cat_name] => test.example.4
[cat_parent] => 2
[cat_slug] => Test Example 4
)
[2] => Array
(
[cat_id] => 2
[cat_name] => test.example.5
[cat_parent] => 0
[cat_slug] => Test Example 5
)
)
解决方案
function flatten($array, &$newArray) {
foreach($array as $i => $el) {
if (!is_array($array)) {
return;
}
if (isset($el['cat_id'])) {
$newArray[$i] = $el;
} else (is_array($el)) {
flatten($el, $newArray);
}
}
}
$result = array();
flatten($array, $result);
这篇关于未知深度的多维数组一dimensonal阵列和PHP相关的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!