简体   繁体   English

从数组中提取特定数据

[英]Pull specific data from array

Currently I pull this data from an array 目前,我从数组中提取此数据

Array
(
    [summonerId] => 19936953
    [modifyDate] => 1394886787000
    [champions] => Array
        (
            [0] => Array
                (
                    [id] => 76
                    [name] => Nidalee
                    [stats] => Array
                        (
                            [totalSessionsPlayed] => 1
                            [totalSessionsLost] => 1
                            [totalSessionsWon] => 0
                            [totalChampionKills] => 1
                            [totalDamageDealt] => 22680
                            [totalDamageTaken] => 12406
                            [mostChampionKillsPerSession] => 1
                            [totalMinionKills] => 14
                            [totalDoubleKills] => 0
                            [totalTripleKills] => 0
                            [totalQuadraKills] => 0
                            [totalPentaKills] => 0
                            [totalUnrealKills] => 0
                            [totalDeathsPerSession] => 6
                            [totalGoldEarned] => 5496
                            [mostSpellsCast] => 0
                            [totalTurretsKilled] => 0
                            [totalPhysicalDamageDealt] => 5232
                            [totalMagicDamageDealt] => 17447
                            [totalFirstBlood] => 0
                            [totalAssists] => 3
                            [maxChampionsKilled] => 1
                            [maxNumDeaths] => 6
                        )

                )

            [1] => Array
                (
                    [id] => 36
                    [name] => DrMundo
                    [stats] => Array
                        (
                            [totalSessionsPlayed] => 1
                            [totalSessionsLost] => 1
                            [totalSessionsWon] => 0
                            [totalChampionKills] => 1
                            [totalDamageDealt] => 89170
                            [totalDamageTaken] => 20817
                            [mostChampionKillsPerSession] => 1
                            [totalMinionKills] => 152
                            [totalDoubleKills] => 0
                            [totalTripleKills] => 0
                            [totalQuadraKills] => 0
                            [totalPentaKills] => 0
                            [totalUnrealKills] => 0
                            [totalDeathsPerSession] => 3
                            [totalGoldEarned] => 8401
                            [mostSpellsCast] => 0
                            [totalTurretsKilled] => 0
                            [totalPhysicalDamageDealt] => 24456
                            [totalMagicDamageDealt] => 64544
                            [totalFirstBlood] => 0
                            [totalAssists] => 2
                            [maxChampionsKilled] => 1
                            [maxNumDeaths] => 3
                        )

                )

I only want to pull ID name totalsessionsplayed , totalsessionslost and totalsessionswon from each array and display it in a table. 我只想从每个数组中提取ID名称totalsessionsplayedtotalsessionslosttotalsessionswon并将其显示在表中。

I have not yet found a way to make it work. 我还没有找到一种使它工作的方法。

I think this is what you're looking for: 我认为这是您要寻找的:

echo '<table>';
foreach($arr['champions'] as $entry) {
    echo '<tr>';
    echo '<td>' . $entry['id'] . '</td>';
    echo '<td>' . $entry['name'] . '</td>';
    echo '<td>' . $entry['stats']['totalSessionsPlayed'] . '</td>';
    echo '<td>' . $entry['stats']['totalSessionsLost'] . '</td>';
    echo '<td>' . $entry['stats']['totalSessionsWon'] . '</td>';
    echo '</tr>';
}
echo '</table>';

You would access your values like, starting from the champions sub array, then iterate through it with a foreach() : 您将访问您的值,例如,从champions子数组开始,然后使用foreach()遍历它:

<?php 
$your_array = ...

foreach($your_array['champions'] as $row){

    //get the values, dont really need to reassign but you get the idea
    $id = $row['id'];
    $name = $row['name'];
    $played = $row['stats']['totalSessionsPlayed'];
    $lost = $row['stats']['totalSessionsLost'];
    $won = $row['stats']['totalSessionsWon'];

    //do something with them values on each iteration
}
?>

You can do a foreach loop in php to get the data you need. 您可以在php中执行foreach循环以获取所需的数据。 I am using $array as the name of your variable where you are holding the array: 我使用$ array作为保存数组的变量名:

foreach($array['champions'] as $thischamp) {
$id = $thischamp['id'];
$name = $thischamp['name'];
$totalsessions = $thischamp['stats']['totalsessionsplayed'];
$totallost = $thischamp['stats']['totalsessionslost'];
$totalwon = $thischamp['stats']['totalsessionswon'];
// now write your html
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM