我试图做一个与array_shift_recursive()等价的工作,显然我的谷歌搜索失败了。
取以下多维数组连续字符串:
$array = [
'shift6th' => [
'shift3rd' => [
'shift1st', //string
'shift2nd', //string
],
'shift4rd', //string
'shitf5th', //string
],
'shift11th' => [
'shift10th', //string
'shift9th' => [
'shift7th', //string
'shift8th', //string
],
],
'shift12th', //string
];关键是以递归的方式返回和删除end元素,因为数组可以有许多(无限的)维度。我已经按我想要转换的顺序命名了字符串和键。
我试图复制一个函数,但它无法工作,因为array_shift返回的是元素,而不是数组,因此很难用函数恢复维度。这不起作用,并一直返回我的shift1st (我期望的),但数组保持不变,不会被移动。
function array_shift_recursive(array $array) {
// recurse 1st child if it's array
if( !empty($array) && is_array(reset($array)) ){
return array_shift_recursive(reset($array));
}
//shift element from current array.
else{
return array_shift($array);
}
}我知道这一定是关于参考的。知道引用是如何工作的,我仍然需要学习如何以及何时利用它。这就是说,很少提示array_shift(&$数组);参数是通过引用传递的。
我找到了一种方法,通过将值切换为false,然后过滤空元素的数组(将false计算为false),但这似乎(对我来说)太长,效率很低。如果你愿意的话,请阅读下面的内容,但重点是要避免这样做;)
function array_filter_recursive(array $array, $filter = null) {
foreach( $array as &$v ){
if( !empty($v) && is_array($v) ){
$v = array_filter_recursive($v, $filter);
}
}
return array_filter($array, $filter);
}
function isnot_empty(&$v) {
if( !empty($v) ){
return true;
}
return false;
}
function turn_false(array $array = []) {
foreach( $array as $k => &$v ){
if( !empty($v) && is_array($v) ){
$array[ $k ] = turn_false($v);
break;
}
else{
$array[ $k ] = false;
break;
}
}
return $array;
}然后我就可以
//This for now gives me the element i'm looking for as expected
$shifted=array_shift_recursive($array);
/** @TODO : get rid of this to remove the shifted element. */
//Replicate the top dimension path with a empty end element.
$empty=turn_false($array);
//Replace the array'S dimension with the empty dimension path
$replaced=array_replace_recursive($array,$empty);
//Remove the empty element (as a bonus it also strip empty array left behind after removing the element)
$array=array_filter_recursive($replaced,'isnot_empty');
/* OR (how i use it to combine those 3 lines in (kind of) one)
$array = array_filter_recursive(
array_replace_recursive(
$array, turn_false($array)
), 'isnot_empty'
);
*/发布于 2017-04-13 04:12:42
我使用与以前相同的逻辑,除了使用foreach循环而不是重置,以获得数组中的第一个元素。只重置标记,并在指针处返回元素,因此我猜它再次复制。
如果要跳过空维度,可以在重新运行函数之前使用array_filter筛选空元素。
感谢评论员们的提醒。
包含此输入:
$array = [
'abc' => [
'def',
'ghi' => [
'jkl', 'mno'
],
],
'pqr' => [
'stu', 'vwx'
],
'yz1',
'234' => [ '456' => [ '789' ] ]
];使用此功能:
function array_shift_recursive(array &$array) {
foreach( $array as &$v ){
if( (!empty($v) && is_array($v) ) ){
return array_shift_recursive($v);
}
else{
return array_shift($array);
}
}
}我们看到的预期结果如下:
var_dump(array_shift_recursive($array)); //string(3) "def"
var_dump(array_shift_recursive($array)); //string(3) "jkl"
var_dump(array_shift_recursive($array)); //string(3) "mno"
var_dump(array_shift_recursive($array)); //array(0) { } //dimension 'ghi'
var_dump(array_shift_recursive($array)); //array(0) { } //dimension 'abc'
var_dump(array_shift_recursive($array)); //string(3) "stu"
var_dump(array_shift_recursive($array)); //string(3) "vwx"
var_dump(array_shift_recursive($array)); //array(0) { } //dimension 'pqr'
var_dump(array_shift_recursive($array)); //string(3) "yz1"
var_dump(array_shift_recursive($array)); //string(3) "789"
var_dump(array_shift_recursive($array)); //array(0) { } //dimension '456'
var_dump(array_shift_recursive($array)); //array(0) { } //dimension '234'
var_dump(array_shift_recursive($array)); //NULL // Array is empty (yay!)https://stackoverflow.com/questions/43364171
复制相似问题