Php 如何获取数组中的值? $playerId=array(); $playerId[]=intval($row['Id']); $allrounders[]=数组( “重量”=>fullTotal美元, 'Id'=>$playerId ); rsort($allrounders); $sliceAr=array\u slice($allrounders,0,5); foreach($sliceAr作为$allroundK){ echo$allrounders[][Id']。“”; }

Php 如何获取数组中的值? $playerId=array(); $playerId[]=intval($row['Id']); $allrounders[]=数组( “重量”=>fullTotal美元, 'Id'=>$playerId ); rsort($allrounders); $sliceAr=array\u slice($allrounders,0,5); foreach($sliceAr作为$allroundK){ echo$allrounders[][Id']。“”; },php,mysql,arrays,Php,Mysql,Arrays,问题: 在上面的数组中,如何相应地获取Id键的值?它获取所有玩家的分数,并用他的ID组织这些分数,然后按降序排序。它需要前5个结果。我需要它们的ID。在foreach循环中,$allroundK是数组的项。在本例中,它是具有权重和id的数组。因此: $playerId= array(); $playerId[] = intval($row['Id']); $allrounders[] = array( 'Weight'=>$fullTotal, 'Id'=>$

问题:
在上面的数组中,如何相应地获取Id键的值?它获取所有玩家的分数,并用他的ID组织这些分数,然后按降序排序。它需要前5个结果。我需要它们的ID。

在foreach循环中,
$allroundK
是数组的项。在本例中,它是具有权重和id的数组。因此:

$playerId= array();
$playerId[] = intval($row['Id']);
$allrounders[] = array(
        'Weight'=>$fullTotal,
    'Id'=>$playerId
);
rsort($allrounders);
$sliceAr = array_slice($allrounders,0,5);
foreach($sliceAr as $allroundK){
         echo $allrounders[]['Id']."<br/>";

}
foreach($sliceAr作为$allroundK){
echo$allroundK['Id']。“
”; }
Do

因为您已经以这种方式设置了数组

echo $allrounders[0]['Id'][0];
这里
$allrounders[]
还意味着数组中的元素权重和Id将添加到数组的第[0]个元素
$allrounders

如果要删除[0],只需按如下方式设置数组

$allrounders[] = array(
    'Weight'=>$fullTotal,
    'Id'=>$playerId
);
现在您可以访问
Id

$allrounders = array(
    'Weight'=>$fullTotal,
    'Id'=>$playerId
);
编辑:

在你的情况下,它将作为

echo $allrounders['Id'][0];
foreach($sliceAr作为$allroundK){
echo$allroundK['Id'][0]。“
”; }

foreach($sliceAr作为$allroundK){
foreach($allroundK['Id'][0]作为$allroundJ){
echo$allroundJ.“
”; } }
从中我只得到了结果中的数组、数组、数组
echo $allrounders['Id'][0];
foreach($sliceAr as $allroundK){
    echo $allroundK['Id'][0]."<br/>";
}
foreach($sliceAr as $allroundK){
    foreach($allroundK['Id'][0] as $allroundJ){
        echo $allroundJ."<br/>";
    }
}