Reputation: 342
This is the current code setup I have which you see prints out each value in a table cell.
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>";
}
I don't really know how to explain it but. Uhhhh what I want is to use this for an individual value since this only works best for multiple values. 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.
}
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.
Dunno what it is called so sorry if my question does not explain it properly/unclear. You can tell me how to revise it though.
Also one last thing feel free to leave me any good references that you guys have in mind. I will gladly read through it and learn a lot from it. :)
NOTE: I think the right/proper question is: How do I make the $member or something work individually and just get rid of the foreach loop altogether.
Upvotes: 1
Views: 140
Reputation: 23670
Since PHP 5.4 you can just do echo $LS::getClan()[0]["username"];
(ref)
Example:
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"];
Mike 10
Bob 9
Mike
Upvotes: 1
Reputation: 2877
$LS::getClan()
appears to return an array of members, consider the following example
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;
}
output
<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>
access a specific element in the array
/**
* 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;
output
<tr><td><label class='label label-danger'>3 | <span>11</span></label></td><td>bunnyLover</td><td>41</td><td>66 </td></tr>
Upvotes: 0