用'foreach`代替单个数组值php

时间:2015-06-04 01:46:00

标签: php arrays foreach

这是我目前的代码设置,您可以看到打印出表格单元格中的每个值。

foreach($LS::getClan() as $member) { 
    echo "<tr><td><label class='label label-danger'>{$member['rank']} | <span>{$member['level']}</span></label></td><td>{$member['username']}</td><td>{$member['donated']}</td><td>{$member['received']} </td></tr>"; 
}

我真的不知道如何解释它,但是。呃我想要的是将它用于单个值,因为这仅适用于多个值。 E.g:

if ($LS::getClan()['username']) {
    // So this code does not work.
    // I hope you guys are seeing what I want cause I don't really know how to explain it.
    // As you can see $LS::getClan()['username'] does not work as for $members['username'] it works just fine and returns the proper value.
}

所以这段代码不起作用。我希望你们看到我想要的东西因为我不知道如何解释它。正如您所看到的,$LS::getClan()['username']不适用于$members['username'],它可以正常工作并返回正确的值。

如果我的问题没有正确/不清楚地解释,那么不知道它叫什么。你可以告诉我如何修改它。

最后还有一件事可以随意给我留下你们想到的任何好的参考资料。我很乐意通读它并从中学到很多东西。 :)

注意:我认为正确/正确的问题是:如何使$ member或者某些东西单独工作,并完全摆脱foreach循环。

2 个答案:

答案 0 :(得分:1)

自PHP 5.4起,您可以echo $LS::getClan()[0]["username"];ref

示例:

class LS {
    private static $array = array(array("username" => "Mike", "rank" => 10),array("username" => "Bob", "rank" => 9));
    public static function getClan() {
        return self::$array;    
    }
}

$LS = 'LS';
foreach($LS::getClan() as $member) { 
    echo $member["username"] . " " . $member["rank"] . "<br>".PHP_EOL;
}

echo $LS::getClan()[0]["username"];
  迈克10   鲍勃9   迈克

答案 1 :(得分:0)

$LS::getClan()似乎返回一组成员,请考虑以下示例

  

sample.php

/**
 * list of clan members
 */
$clanMembers = [
    [
        'username' => 'ninjaDude',
        'rank' => 1,
        'level' => 27,
        'donated' => 77,
        'received' => 55
    ],
    [
        'username' => 'rabbitKiller',
        'rank' => 2,
        'level' => 12,
        'donated' => 555,
        'received' => 22
    ],
    [
        'username' => 'bunnyLover',
        'rank' => 3,
        'level' => 11,
        'donated' => 41,
        'received' => 66
    ]
];

foreach($clanMembers as $member) {
    echo "<tr><td><label class='label label-danger'>{$member['rank']} | <span>{$member['level']}</span></label></td><td>{$member['username']}</td><td>{$member['donated']}</td><td>{$member['received']} </td></tr>" . PHP_EOL;
}
  

输出

<tr><td><label class='label label-danger'>1 | <span>27</span></label></td><td>ninjaDude</td><td>77</td><td>55 </td></tr>
<tr><td><label class='label label-danger'>2 | <span>12</span></label></td><td>rabbitKiller</td><td>555</td><td>22 </td></tr>
<tr><td><label class='label label-danger'>3 | <span>11</span></label></td><td>bunnyLover</td><td>41</td><td>66 </td></tr>
  

访问数组中的特定元素

/**
  * this will pick the third member in the array, 
  * this is because the count starts at zero
  */
$member = $clanMembers[2];

echo "<tr><td><label class='label label-danger'>{$member['rank']} | <span>{$member['level']}</span></label></td><td>{$member['username']}</td><td>{$member['donated']}</td><td>{$member['received']} </td></tr>" . PHP_EOL;
  

输出

<tr><td><label class='label label-danger'>3 | <span>11</span></label></td><td>bunnyLover</td><td>41</td><td>66 </td></tr>