1

我有一组嵌套数组。

[data] 是顶级数组。

较低级别的数组都由增量数字标识,动态分配......基本上每个子数组标识信息的行/记录。

我将如何提取子数组的计数?我需要能够将这些数据发布到数据库中。

子数组的数量是动态的,并且可以根据我正在解析的数据而改变。

 [data] => Array
        (
            [0] => Array
                (
                    [id] => 3475
                    [name] => Player1
                    [score] => 11870
                    [rank] => 213
                    [total_cities] => 2
                    [crown] => None
                    [alliance_id] => 134
                    [title] => Earl
                    [fame_total] => 509875
                    [fame_rank] => 381
                    [defeated_total] => 3436
                    [defeated_rank] => 376
                    [plunder_total] => 1388754
                    [plunder_rank] => 245
                )

            [1] => Array
                (
                    [id] => 3523
                    [name] => Player2
                    [score] => 1978
                    [rank] => 281
                    [total_cities] => 1
                    [crown] => None
                    [alliance_id] => 134
                    [title] => Baron
                    [fame_total] => 0
                    [fame_rank] => 448
                    [defeated_total] => 0
                    [defeated_rank] => 448
                    [plunder_total] => 0
                    [plunder_rank] => 436
                )

            [2] => Array
                (
                    [id] => 73
                    [name] => Player3
                    [score] => 1308
                    [rank] => 304
                    [total_cities] => 1
                    [crown] => None
                    [alliance_id] => 134
                    [title] => Marquess
                    [fame_total] => 5604153
                    [fame_rank] => 237
                    [defeated_total] => 37270
                    [defeated_rank] => 229
                    [plunder_total] => 68130
                    [plunder_rank] => 335
                )
4

3 回答 3

2

我结合使用 Marc B 和 Thanos 强调的方法来获得我想要的结果

这是我使用的代码:

$count = 0;
foreach($data as $data=>$inner_array){
   $count = $count+1;
}

echo $count;

我用一个包含 143 个唯一行的测试数据集运行它,我得到了回声,所以生活很美好。

多谢你们。

于 2013-04-03T16:23:32.837 回答
0

最简单的方法:

$count = 0;
foreach($mainarray as $subarray)
   $count += count($subarray);
}
于 2013-04-03T15:16:23.130 回答
0

假设 $data 是您的主数组:这个打印每个内部数组的名称(例如,0、1、2...),然后计算每个子数组项:

echo "Number of sub-arrays: ".count($online_time); //optional
foreach($data as $inner_array=>$data_of_inner_array) {
echo $inner_array;
echo " --- ".count($data_of_inner_array);
echo "<br/>";
}

我假设输出将是

Number of sub-arrays: 3
0 --- 14
1 --- 14
2 --- 14
于 2013-04-03T15:28:55.593 回答