如何切割大数组并根据参数page_no的值返回切片数组?

时间:2015-03-11 07:23:54

标签: php arrays multidimensional-array associative-array key-value

我有一个名为$data的大阵。作为参考,我只把它的14个元素放在下面。实际上它可能包含数百个这样的元素:

Array
(
    [0] => 16
    [1] => Array
        (
            [0] => Array
                (
                    [user_id] => 199
                    [profile_page_id] => 0
                    [user_server_id] => 0
                    [user_name] => profile-199
                    [full_name] => Benjamin Jordon
                    [gender] => 0
                    [user_image] => 
                    [is_invisible] => 0
                    [user_group_id] => 6
                    [language_id] => 
                )

            [1] => Array
                (
                    [user_id] => 901
                    [profile_page_id] => 0
                    [user_server_id] => 1
                    [user_name] => profile-901
                    [full_name] => Campusknot .
                    [gender] => 0
                    [user_image] => 2014/11/b23e023750785c8b5e61ace4d6a202fa%s.png
                    [is_invisible] => 0
                    [user_group_id] => 6
                    [language_id] => 
                )

            [2] => Array
                (
                    [user_id] => 244
                    [profile_page_id] => 0
                    [user_server_id] => 0
                    [user_name] => profile-244
                    [full_name] => Campusknot .
                    [gender] => 0
                    [user_image] => 2015/02/0f34dbb95ce18b17611220b78c87f9a6%s.png
                    [is_invisible] => 0
                    [user_group_id] => 7
                    [language_id] => 
                )

            [3] => Array
                (
                    [user_id] => 200
                    [profile_page_id] => 0
                    [user_server_id] => 0
                    [user_name] => profile-200
                    [full_name] => Derius Galvez
                    [gender] => 0
                    [user_image] => 
                    [is_invisible] => 0
                    [user_group_id] => 6
                    [language_id] => 
                )

            [4] => Array
                (
                    [user_id] => 109
                    [profile_page_id] => 0
                    [user_server_id] => 0
                    [user_name] => profile-109
                    [full_name] => Hiten patel
                    [gender] => 0
                    [user_image] => 
                    [is_invisible] => 0
                    [user_group_id] => 6
                    [language_id] => 
                )

            [5] => Array
                (
                    [user_id] => 204
                    [profile_page_id] => 0
                    [user_server_id] => 0
                    [user_name] => profile-204
                    [full_name] => Huong Nguyen
                    [gender] => 0
                    [user_image] => 
                    [is_invisible] => 0
                    [user_group_id] => 6
                    [language_id] => 
                )

            [6] => Array
                (
                    [user_id] => 196
                    [profile_page_id] => 0
                    [user_server_id] => 1
                    [user_name] => profile-196
                    [full_name] => Ira Hampton
                    [gender] => 1
                    [user_image] => 2014/11/24c4a6835e667b67b82cea3666841ac7%s.jpg
                    [is_invisible] => 0
                    [user_group_id] => 6
                    [language_id] => 
                )

            [7] => Array
                (
                    [user_id] => 211
                    [profile_page_id] => 0
                    [user_server_id] => 0
                    [user_name] => profile-211
                    [full_name] => Kevin Rolando Vilcapaza Diaz
                    [gender] => 1
                    [user_image] => 
                    [is_invisible] => 0
                    [user_group_id] => 6
                    [language_id] => 
                )

            [8] => Array
                (
                    [user_id] => 202
                    [profile_page_id] => 0
                    [user_server_id] => 1
                    [user_name] => profile-202
                    [full_name] => Mai Le
                    [gender] => 0
                    [user_image] => 2014/08/ebaf98a8b7875b4814b06baceaccad22%s.jpg
                    [is_invisible] => 0
                    [user_group_id] => 6
                    [language_id] => 
                )

            [9] => Array
                (
                    [user_id] => 185
                    [profile_page_id] => 0
                    [user_server_id] => 1
                    [user_name] => profile-185
                    [full_name] => Perceus Mody
                    [gender] => 1
                    [user_image] => 2014/09/5f86df8752fd7079296c0101d235a592%s.jpg
                    [is_invisible] => 0
                    [user_group_id] => 6
                    [language_id] => 
                )

            [10] => Array
                (
                    [user_id] => 205
                    [profile_page_id] => 0
                    [user_server_id] => 1
                    [user_name] => profile-205
                    [full_name] => Phong Phan
                    [gender] => 0
                    [user_image] => 2014/08/d32361f1f72a4e24b9c8329501bc8aa1%s.jpg
                    [is_invisible] => 0
                    [user_group_id] => 6
                    [language_id] => 
                )

            [11] => Array
                (
                    [user_id] => 201
                    [profile_page_id] => 0
                    [user_server_id] => 1
                    [user_name] => profile-201
                    [full_name] => Rahul Gopal
                    [gender] => 1
                    [user_image] => 2014/09/d936e72dbad21732812c33e80752aad6%s.png
                    [is_invisible] => 0
                    [user_group_id] => 6
                    [language_id] => 
                )

            [12] => Array
                (
                    [user_id] => 208
                    [profile_page_id] => 0
                    [user_server_id] => 0
                    [user_name] => profile-208
                    [full_name] => SHUVAM SAHA
                    [gender] => 0
                    [user_image] => 
                    [is_invisible] => 0
                    [user_group_id] => 6
                    [language_id] => 
                )

            [13] => Array
                (
                    [user_id] => 212
                    [profile_page_id] => 0
                    [user_server_id] => 0
                    [user_name] => profile-212
                    [full_name] => Turner Corbin
                    [gender] => 0
                    [user_image] => 
                    [is_invisible] => 0
                    [user_group_id] => 6
                    [language_id] => 
                )

        )

)

从上面的数组中,请忽略第一个键值对,即[0] => 16。考虑[1]中包含14个元素的数组。

现在我想要的是一个函数,我将传递数组$data和参数page_no

假设,如果我已经通过了page_no=1那么我应该获得前10条记录,即以下数组:

Array
    (

                [0] => Array
                    (
                        [user_id] => 199
                        [profile_page_id] => 0
                        [user_server_id] => 0
                        [user_name] => profile-199
                        [full_name] => Benjamin Jordon
                        [gender] => 0
                        [user_image] => 
                        [is_invisible] => 0
                        [user_group_id] => 6
                        [language_id] => 
                    )

                [1] => Array
                    (
                        [user_id] => 901
                        [profile_page_id] => 0
                        [user_server_id] => 1
                        [user_name] => profile-901
                        [full_name] => Campusknot .
                        [gender] => 0
                        [user_image] => 2014/11/b23e023750785c8b5e61ace4d6a202fa%s.png
                        [is_invisible] => 0
                        [user_group_id] => 6
                        [language_id] => 
                    )

                [2] => Array
                    (
                        [user_id] => 244
                        [profile_page_id] => 0
                        [user_server_id] => 0
                        [user_name] => profile-244
                        [full_name] => Campusknot .
                        [gender] => 0
                        [user_image] => 2015/02/0f34dbb95ce18b17611220b78c87f9a6%s.png
                        [is_invisible] => 0
                        [user_group_id] => 7
                        [language_id] => 
                    )

                [3] => Array
                    (
                        [user_id] => 200
                        [profile_page_id] => 0
                        [user_server_id] => 0
                        [user_name] => profile-200
                        [full_name] => Derius Galvez
                        [gender] => 0
                        [user_image] => 
                        [is_invisible] => 0
                        [user_group_id] => 6
                        [language_id] => 
                    )

                [4] => Array
                    (
                        [user_id] => 109
                        [profile_page_id] => 0
                        [user_server_id] => 0
                        [user_name] => profile-109
                        [full_name] => Hiten patel
                        [gender] => 0
                        [user_image] => 
                        [is_invisible] => 0
                        [user_group_id] => 6
                        [language_id] => 
                    )

                [5] => Array
                    (
                        [user_id] => 204
                        [profile_page_id] => 0
                        [user_server_id] => 0
                        [user_name] => profile-204
                        [full_name] => Huong Nguyen
                        [gender] => 0
                        [user_image] => 
                        [is_invisible] => 0
                        [user_group_id] => 6
                        [language_id] => 
                    )

                [6] => Array
                    (
                        [user_id] => 196
                        [profile_page_id] => 0
                        [user_server_id] => 1
                        [user_name] => profile-196
                        [full_name] => Ira Hampton
                        [gender] => 1
                        [user_image] => 2014/11/24c4a6835e667b67b82cea3666841ac7%s.jpg
                        [is_invisible] => 0
                        [user_group_id] => 6
                        [language_id] => 
                    )

                [7] => Array
                    (
                        [user_id] => 211
                        [profile_page_id] => 0
                        [user_server_id] => 0
                        [user_name] => profile-211
                        [full_name] => Kevin Rolando Vilcapaza Diaz
                        [gender] => 1
                        [user_image] => 
                        [is_invisible] => 0
                        [user_group_id] => 6
                        [language_id] => 
                    )

                [8] => Array
                    (
                        [user_id] => 202
                        [profile_page_id] => 0
                        [user_server_id] => 1
                        [user_name] => profile-202
                        [full_name] => Mai Le
                        [gender] => 0
                        [user_image] => 2014/08/ebaf98a8b7875b4814b06baceaccad22%s.jpg
                        [is_invisible] => 0
                        [user_group_id] => 6
                        [language_id] => 
                    )

                [9] => Array
                    (
                        [user_id] => 185
                        [profile_page_id] => 0
                        [user_server_id] => 1
                        [user_name] => profile-185
                        [full_name] => Perceus Mody
                        [gender] => 1
                        [user_image] => 2014/09/5f86df8752fd7079296c0101d235a592%s.jpg
                        [is_invisible] => 0
                        [user_group_id] => 6
                        [language_id] => 
                    )           

    )

然后,如果我传递page_no = 2,那么随后的一组或多个记录,即[10],[11],[12],[13]键应该得到回报。数组键不应再从[0]开始。换句话说,我希望基于参数page_no的值,包含10个或更少元素的数组元素,原始数组键完好无损。

如果特定页码没有元素,那么它应该返回false。

我应该如何以有效的方式做到这一点,因为实际的数组可能是非常庞大的,有数百个这样的元素?请帮我。谢谢。

2 个答案:

答案 0 :(得分:0)

使用: -

$input = array('', ................);

$pageno = 1
$numberofrecords = 10;


$array_end = $pageno * 10;
$array_e= $array_end  -  1;



$numbers =  $numberofrecords - 1;
$array_start = $array_e - $numbers;


print_r(array_slice($input,$array_start,$array_e));

答案 1 :(得分:0)

你可以试试这个:

function slice($data = array(), $pageNo = null)
{
    if (is_null($data)) {
        return array();
    } else {
        if ($pageNo == null || $pageNo == 1) {
            $ctr = 0;
            for ($i = 0; $i < 10; $i++) {
                if (isset($data[$ctr])) {
                    $dataSliced[] = $data[$ctr];
                    $ctr++;
                }
            }
        } else {
            $ctr = ($pageNo * 10) - 10;
            for ($i = 0; $i < 10; $i++) {
               if (isset($data[$ctr])) {
                  $dataSliced[] = $data[$ctr];
                  $ctr++;
               }
            }
        }
        return $dataSliced;
    }
}

此函数已检查$data为空并将返回一个空数组,否则它将检查控制将返回的数组的$pageNo,如果$pageNo为空或1 $ctr是控制变量,它知道它将获得的数据的索引,因此它将获得数据的索引0,否则如果索引是2,它将获得下一个10条记录的索引,依此类推。希望它有所帮助。

或者如果你想要php的array_slice方法,你可以试试这个:

function slice($data = array(), $pageNo = null)
{
    if (is_null($data)) {
        return array();
    } else {
        if ($pageNo) {
            $ctr = ($pageNo * 10) - 10;
            $dataSliced = array_slice($data, $ctr, 10); 
        }
        return $dataSliced;
    }
}