如何从多维组中获取密钥到密钥之间的总数组计数

时间:2016-01-12 12:59:10

标签: php arrays multidimensional-array

我有以下多维组数组,并希望从2键到3键获得总数组数。

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [MessageIdx] => 4
                    [ID] => jack@example.com
                    [FirstName] => Jack
                    [LastName] => Jack
                    [Nickname] => Jack
                    [PhotoFilePathName] => 1452504870.png
                    [Text] =>
                    [PostedAt] => 2016-01-01 05:12:17
                    [NumLikes] => 49
                )

            [1] => Array
                (
                    [MessageIdx] => 5
                    [ID] => tom@example.com
                    [FirstName] => Tom
                    [LastName] => 
                    [Nickname] => Tom
                    [PhotoFilePathName] => 
                    [Text] =>
                    [PostedAt] => 2016-01-01 05:25:29
                    [NumLikes] => 14
                )

            [2] => Array
                (
                    [MessageIdx] => 6
                    [ID] => tom@example.com
                    [FirstName] => Tom
                    [LastName] => 
                    [Nickname] => Tom
                    [PhotoFilePathName] => 
                    [Text] => 
                    [PostedAt] => 2016-01-02 05:38:37
                    [NumLikes] => 5
                )

            [3] => Array
                (
                    [MessageIdx] => 7
                    [ID] => jack@example.com
                    [FirstName] => Jack
                    [LastName] => Jack
                    [Nickname] => Jack
                    [PhotoFilePathName] => 1452504870.png
                    [Text] => 
                    [PostedAt] => 2016-01-02 08:38:37
                    [NumLikes] => 64
                )

            [4] => Array
                (
                    [MessageIdx] => 8
                    [ID] => tom@example.com
                    [FirstName] => Tom
                    [LastName] => 
                    [Nickname] => Tom
                    [PhotoFilePathName] => 
                    [Text] => 
                    [PostedAt] => 2016-01-03 10:38:37
                    [NumLikes] => 54
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [MessageIdx] => 9
                    [ID] => jack@example.com
                    [FirstName] => Jack
                    [LastName] => Jack
                    [Nickname] => Jack
                    [PhotoFilePathName] => 1452504870.png
                    [Text] => 
                    [PostedAt] => 2016-01-03 12:49:47
                    [NumLikes] => 19
                )

            [1] => Array
                (
                    [MessageIdx] => 10
                    [ID] => tom@example.com
                    [FirstName] => Tom
                    [LastName] => 
                    [Nickname] => Tom
                    [PhotoFilePathName] => 
                    [Text] => 
                    [PostedAt] => 2016-01-04 06:23:26
                    [NumLikes] => 5
                )

            [2] => Array
                (
                    [MessageIdx] => 11
                    [ID] => tom@example.com
                    [FirstName] => Tom
                    [LastName] => 
                    [Nickname] => Tom
                    [PhotoFilePathName] => 
                    [Text] =>
                    [PostedAt] => 2016-01-04 10:36:34
                    [NumLikes] => 38
                )

            [3] => Array
                (
                    [MessageIdx] => 12
                    [ID] => tom@example.com
                    [FirstName] => Tom
                    [LastName] => 
                    [Nickname] => Tom
                    [PhotoFilePathName] => 
                    [Text] =>
                    [PostedAt] => 2016-01-05 10:36:34
                    [NumLikes] => 98
                )

            [4] => Array
                (
                    [MessageIdx] => 13
                    [ID] => jack@example.com
                    [FirstName] => Jack
                    [LastName] => Jack
                    [Nickname] => Jack
                    [PhotoFilePathName] => 1452504870.png
                    [Text] =>
                    [PostedAt] => 2016-01-05 14:18:41
                    [NumLikes] => 87
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [MessageIdx] => 14
                    [ID] => jack@example.com
                    [FirstName] => Jack
                    [LastName] => Jack
                    [Nickname] => Jack
                    [PhotoFilePathName] => 1452504870.png
                    [Text] => 
                    [PostedAt] => 2016-01-06 09:29:17
                    [NumLikes] => 59
                )

            [1] => Array
                (
                    [MessageIdx] => 15
                    [ID] => tom@example.com
                    [FirstName] => Tom
                    [LastName] => 
                    [Nickname] => Tom
                    [PhotoFilePathName] => 
                    [Text] =>
                    [PostedAt] => 2016-01-06 09:29:17
                    [NumLikes] => 72
                )

            [2] => Array
                (
                    [MessageIdx] => 16
                    [ID] => jack@example.com
                    [FirstName] => Jack
                    [LastName] => Jack
                    [Nickname] => Jack
                    [PhotoFilePathName] => 1452504870.png
                    [Text] =>
                    [PostedAt] => 2016-01-06 12:39:33
                    [NumLikes] => 69
                )

            [3] => Array
                (
                    [MessageIdx] => 17
                    [ID] => tom@example.com
                    [FirstName] => Tom
                    [LastName] => 
                    [Nickname] => Tom
                    [PhotoFilePathName] => 
                    [Text] =>
                    [PostedAt] => 2016-01-07 06:10:51
                    [NumLikes] => 74
                )

            [4] => Array
                (
                    [MessageIdx] => 18
                    [ID] => jack@example.com
                    [FirstName] => Jack
                    [LastName] => Jack
                    [Nickname] => Jack
                    [PhotoFilePathName] => 1452504870.png
                    [Text] => 
                    [PostedAt] => 2016-01-07 12:28:51
                    [NumLikes] => 19
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [MessageIdx] => 19
                    [ID] => jack@example.com
                    [FirstName] => Jack
                    [LastName] => Jack
                    [Nickname] => Jack
                    [PhotoFilePathName] => 1452504870.png
                    [Text] =>
                    [PostedAt] => 2016-01-08 02:06:10
                    [NumLikes] => 32
                )

            [1] => Array
                (
                    [MessageIdx] => 20
                    [ID] => tom@example.com
                    [FirstName] => Tom
                    [LastName] => 
                    [Nickname] => Tom
                    [PhotoFilePathName] => 
                    [Text] =>
                    [PostedAt] => 2016-01-08 07:15:10
                    [NumLikes] => 47
                )

            [2] => Array
                (
                    [MessageIdx] => 21
                    [ID] => tom@example.com
                    [FirstName] => Tom
                    [LastName] => 
                    [Nickname] => Tom
                    [PhotoFilePathName] => 
                    [Text] =>
                    [PostedAt] => 2016-01-09 11:24:27
                    [NumLikes] => 82
                )

            [3] => Array
                (
                    [MessageIdx] => 22
                    [ID] => tom@example.com
                    [FirstName] => Tom
                    [LastName] => 
                    [Nickname] => Tom
                    [PhotoFilePathName] => 
                    [Text] =>
                    [PostedAt] => 2016-01-09 14:23:08
                    [NumLikes] => 71
                )

        )

)

现在我想在2键到最后一个键3之间启动数组总计。

Array
    (
        [2] => Array
            (
                [0] => Array
                    (
                        [MessageIdx] => 14
                        [ID] => jack@example.com
                        [FirstName] => Jack
                        [LastName] => Jack
                        [Nickname] => Jack
                        [PhotoFilePathName] => 1452504870.png
                        [Text] =>
                        [PostedAt] => 2016-01-06 09:29:17
                        [NumLikes] => 59
                    )

                [1] => Array
                    (
                        [MessageIdx] => 15
                        [ID] => tom@example.com
                        [FirstName] => Tom
                        [LastName] => 
                        [Nickname] => Tom
                        [PhotoFilePathName] => 
                        [Text] =>
                        [PostedAt] => 2016-01-06 09:29:17
                        [NumLikes] => 72
                    )

                [2] => Array
                    (
                        [MessageIdx] => 16
                        [ID] => jack@example.com
                        [FirstName] => Jack
                        [LastName] => Jack
                        [Nickname] => Jack
                        [PhotoFilePathName] => 1452504870.png
                        [Text] =>
                        [PostedAt] => 2016-01-06 12:39:33
                        [NumLikes] => 69
                    )

                [3] => Array
                    (
                        [MessageIdx] => 17
                        [ID] => tom@example.com
                        [FirstName] => Tom
                        [LastName] => 
                        [Nickname] => Tom
                        [PhotoFilePathName] => 
                        [Text] =>
                        [PostedAt] => 2016-01-07 06:10:51
                        [NumLikes] => 74
                    )

                [4] => Array
                    (
                        [MessageIdx] => 18
                        [ID] => jack@example.com
                        [FirstName] => Jack
                        [LastName] => Jack
                        [Nickname] => Jack
                        [PhotoFilePathName] => 1452504870.png
                        [Text] =>
                        [PostedAt] => 2016-01-07 12:28:51
                        [NumLikes] => 19
                    )

            )

        [3] => Array
            (
                [0] => Array
                    (
                        [MessageIdx] => 19
                        [ID] => jack@example.com
                        [FirstName] => Jack
                        [LastName] => Jack
                        [Nickname] => Jack
                        [PhotoFilePathName] => 1452504870.png
                        [Text] =>
                        [PostedAt] => 2016-01-08 02:06:10
                        [NumLikes] => 32
                    )

                [1] => Array
                    (
                        [MessageIdx] => 20
                        [ID] => tom@example.com
                        [FirstName] => Tom
                        [LastName] => 
                        [Nickname] => Tom
                        [PhotoFilePathName] => 
                        [Text] =>
                        [PostedAt] => 2016-01-08 07:15:10
                        [NumLikes] => 47
                    )

                [2] => Array
                    (
                        [MessageIdx] => 21
                        [ID] => tom@example.com
                        [FirstName] => Tom
                        [LastName] => 
                        [Nickname] => Tom
                        [PhotoFilePathName] => 
                        [Text] =>
                        [PostedAt] => 2016-01-09 11:24:27
                        [NumLikes] => 82
                    )

                [3] => Array
                    (
                        [MessageIdx] => 22
                        [ID] => tom@example.com
                        [FirstName] => Tom
                        [LastName] => 
                        [Nickname] => Tom
                        [PhotoFilePathName] => 
                        [Text] =>
                        [PostedAt] => 2016-01-09 14:23:08
                        [NumLikes] => 71
                    )

            )

    )

9密钥到2可以使用3个数组,我希望从数组中获取9个数。

知道如何获得数组的总数吗?

感谢。

3 个答案:

答案 0 :(得分:1)

试试这段代码。

$selected_keys= range(2, 3);
$count= 0;
foreach($selected_keys as $selected_key) {
   $count+= count($arr[$selected_key]);
}
echo $count;

输出

9

答案 1 :(得分:0)

对于您的具体问题,您可以只计算这两个索引并将它们相加:

$total = count($array[2]) + count(array[3]);

如果你想要一个更加动态的解决方案,它可以在密钥x和y之间起作用:

function countRange($array, $start, $end){
    $total = 0;
    foreach(range($start, $end) as $index){
        if(isset($array[$index]) && is_array($array[$index])){
            $total += count($array[$index]);
        }
    }
    return $total;
}

echo 'total: ' . countRange($array, 2, 3);

答案 2 :(得分:0)

如果你正确地知道了键,那么你可以尝试这个 -

$keys= range(2, 3);
$count= 0;

foreach($keys as $key) {
   $count+= count($yourarray[$key]);
}

如果您不确定密钥,也可以添加支票 -

$num = !empty($yourarray[$key]) ? count($yourarray[$key]) : 0;
$count+= $num;