显示数组中特定范围的元素

时间:2014-12-17 21:25:45

标签: php arrays

我有一个如下所示的数组:

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-55-10等)的函数,并显示数组中的元素范围。

例如,范围1-5将仅显示数组中的元素100, 200, 300, 400, and 500。范围5-10会显示元素500, 600, 700, 800, and 900

谢谢!

3 个答案:

答案 0 :(得分:1)

如果我正确地解释它,看起来你需要首先展平数组,然后array_slice将按照你想要的方式工作。

具体来说,就像这样:

function flatten(array $array) {
  $return = array();
  array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
  return $return;
}

取自this question.

答案 1 :(得分:1)

这里你是我的朋友

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);

答案 2 :(得分:0)

与我的第一个答案有些不同,但可能更多的是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;
}