Php 在多维数组中计算行/记录

Php 在多维数组中计算行/记录,php,arrays,dynamic,count,Php,Arrays,Dynamic,Count,我有一组嵌套数组 [data]是顶级阵列 较低级别的数组都由增量数字标识,动态分配…基本上每个子数组标识一行/一条信息记录 如何提取子数组的计数?我需要能够做到这一点,张贴到数据库中的数据 子数组的数量是动态的,并且可以根据我正在解析的数据而变化 [data] => Array ( [0] => Array ( [id] => 3475

我有一组嵌套数组

[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
                )
最简单的方法:

$count = 0;
foreach($mainarray as $subarray)
   $count += count($subarray);
}

假设$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/>";
}

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

这是我使用的代码:

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

echo $count;
我用143个唯一行的测试数据集运行了它,我得到了回音,所以生活是美好的


谢谢大家。

我甚至不确定我是否理解这个问题,但是带递归标志的数组\u count()?或者只需数组计数($myArray['data'])
array\u计数($data)
array\u计数($\u POST['data'])
只需返回一个无属性标志。我只是简单地计算主数组中子数组的数量。我已经弄清楚下面的代码是什么;我的下一步是使用该代码作为循环,在此循环中我将运行SQL语句来插入/更新数据库。什么是无属性标志?当我尝试回显
array\u count($data)
array\u count($\u POST['data'))
的结果时,它在控制台中显示的所有内容都是无属性。我已经在我的代码中定义了
@data=$\u POST['data']
,所以两者都可以工作。当我尝试分配
$count=array\u count($data)
然后执行
echo$count
时,在浏览器控制台中得到的只是没有属性。就这么说。当然,这是在尝试使用您突出显示的方法。如果你仔细阅读,我确实得到了我的最终结果,所以谢谢你的回复。谢谢,但我要做的就是数一数子数组。我该怎么做?我不需要知道子数组中的元素计数,因为这是固定的。它不是第一行吗?“可选项”?在你的例子中,哪一个是预期的结果?预期的结果只是一个直接的计数,我已经通过我上面写的方法实现了它。谢谢你给我指出了正确的方向。
$count = 0;
foreach($data as $data=>$inner_array){
   $count = $count+1;
}

echo $count;