我有一个看起来像这样的数组:
Array
(
[100] => Array
(
[user_id] => 100
[nest] => Array
(
)
)
[200] => Array
(
[user_id] => 200
[nest] => Array
(
[300] => Array
(
[user_id] => 300
[nest] => Array
(
)
)
[400] => Array
(
[user_id] => 400
[nest] => Array
(
)
)
[500] => Array
(
[user_id] => 500
[nest] => Array
(
)
)
)
)
[600] => Array
(
[user_id] => 600
[nest] => Array
(
)
)
[700] => Array
(
[user_id] => 700
[nest] => Array
(
)
)
[800] => Array
(
[user_id] => 800
[nest] => Array
(
)
)
[900] => Array
(
[user_id] => 900
[nest] => Array
(
)
)
)我希望能够一次只显示5个元素。如何创建一个接受范围(1-5或5-10等)的函数?并显示该数组中的元素范围。
例如,范围1-5将仅显示数组中的元素100, 200, 300, 400, and 500。Range 5-10将显示元素500, 600, 700, 800, and 900。
谢谢!
发布于 2014-12-18 06:05:17
如果我没弄错的话,看起来你需要先展平数组,然后array_slice就会按照你想要的方式工作。
具体地说,就像这样:
function flatten(array $array) {
$return = array();
array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
return $return;
}发布于 2014-12-18 06:07:21
你就是我的朋友
function customSlice(array $a, $iStartRange, $iEndRange) {
$aRange = range($iStartRange, $iEndRange);
$oIt = new RecursiveIteratorIterator(
new RecursiveArrayIterator($a),
RecursiveIteratorIterator::SELF_FIRST);
$aResult = array();
foreach($oIt as $name => $element)
if(in_array($name, $aRange))
$aResult[$name] = $element;
return $aResult;
}示例
$aExample = array(
100 => array('user_id' => 100, 'nest' => array()),
200 => array('user_id' => 200, 'nest' => array(
300 => array('user_id' => 300, 'nest' => array()),
400 => array('user_id' => 400, 'nest' => array()),
500 => array('user_id' => 500, 'nest' => array())
)),
600 => array('user_id' => 600, 'nest' => array()),
700 => array('user_id' => 700, 'nest' => array()),
800 => array('user_id' => 800, 'nest' => array()),
900 => array('user_id' => 900, 'nest' => array())
);
$aSliced = customSlice($aExample, 100, 500);发布于 2014-12-18 13:03:33
与我的第一个答案有些不同,但可能更接近OP的目标。此函数将展平数组,并从展平的透视图进行切片。它在内联中进行切片,而不是在展平之后。此外,我已经注释掉了结果中的'nest'元素,但包含了代码,否则结果中可能会有大量冗余数据。最后,我对这一点做了一些不错的评论。享受吧!
/**
* Slice any array based on flattened perspective.
* @param array $a Array to slice
* @param int $iStartRange Starting index to slice (inclusive)
* @param int $iEndRange Ending index to slice (inclusive)
* Return array Sliced subset of input array.
*/
function recursive_array_slice(array $a, $iStartRange, $iEndRange)
{
// Flatten out the inbound array
$oIt = new RecursiveIteratorIterator(
new RecursiveArrayIterator($a),
RecursiveIteratorIterator::SELF_FIRST);
$aResult = array(); // The sliced result
$i = 1; // Track which element we're on
foreach($oIt as $name => $element) {
// Not a very rigorous check, but this filters
// candidates down to ones we're likely looking for
if(!is_int($name))
continue;
// If the current element is in the range of
// interest, include it in the results
if($i >= $iStartRange && $i <= $iEndRange) {
$aResult[$name] = $element;
// Optional if you don't want a ton of redundant
// data in the results
// unset($aResult[$name]['nest']);
}
// Increment the count of the current element
$i++;
// Bail if we've gone past the range of interest
if($i > $iEndRange)
break;
}
return $aResult;
}https://stackoverflow.com/questions/27535126
复制相似问题