从数组中提取特定数据

时间:2014-03-17 20:53:26

标签: php

目前我从数组中提取此数据

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
                        )

                )

我只想从每个数组中提取ID名称totalsessionsplayedtotalsessionslosttotalsessionswon,并将其显示在表格中。

我还没有办法让它发挥作用。

3 个答案:

答案 0 :(得分:0)

我认为这是你正在寻找的东西:

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>';

答案 1 :(得分:0)

您可以从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
}
?>

答案 2 :(得分:0)

你可以在php中做一个foreach循环来获取你需要的数据。我使用$ 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
}