在我正在处理的PHP页面上,我有一个数组,我试图从中获取数据,如下所示:
Array
(
[races] => Array
(
[0] => Array
(
[id] => 1
[mask] => 1
[side] => alliance
[name] => Human
)
[1] => Array
(
[id] => 2
[mask] => 2
[side] => horde
[name] => Orc
)
[2] => Array
(
[id] => 3
[mask] => 4
[side] => alliance
[name] => Dwarf
)
...
我试图根据ID获取[name]的值。我知道如果ID是每个阵列的关键会更容易,但是我从API中提取这个数组并且无法控制它。任何帮助都会非常感激!
答案 0 :(得分:0)
使用foreach循环:
foreach($data['races'] as $item)
{
if($item['id'] == $id) // $id is you id for comparison
{
// add your code here
}
}
答案 1 :(得分:0)
您可以在循环中获取值:
foreach($array['races'] as $race) {
if ($race['id'] == $id) {
$name = $race['name'];
}
// Rest of the method
}