我有这个数组(为了便于阅读而缩短)
array(10) {
[0] => array(23) {
["account_id"] => int(4294967295)["player_slot"] => int(0)["hero_id"] => int(41)["item_0"] => int(0)["item_1"] => int(0)["item_2"] => int(0)["item_3"] => int(0)["item_4"] => int(0)["item_5"] => int(0)["kills"] => int(0)["deaths"] => int(0)["assists"] => int(0)["leaver_status"] => int(1)["gold"] => int(826)["last_hits"] => int(0)["denies"] => int(0)["gold_per_min"] => int(1799)["xp_per_min"] => int(0)["gold_spent"] => int(0)["hero_damage"] => int(0)["tower_damage"] => int(0)["hero_healing"] => int(0)["level"] => int(1)
}[1] => array(23) {
["account_id"] => int(101863674)["player_slot"] => int(1)["hero_id"] => int(0)["item_0"] => int(0)["item_1"] => int(0)["item_2"] => int(0)["item_3"] => int(0)["item_4"] => int(0)["item_5"] => int(0)["kills"] => int(0)["deaths"] => int(0)["assists"] => int(0)["leaver_status"] => int(1)["gold"] => int(0)["last_hits"] => int(0)["denies"] => int(0)["gold_per_min"] => int(1799)["xp_per_min"] => int(0)["gold_spent"] => int(0)["hero_damage"] => int(0)["tower_damage"] => int(0)["hero_healing"] => int(0)["level"] => int(0)
}[2] => array(23) {
["account_id"] => int(112698809)["player_slot"] => int(2)["hero_id"] => int(0)["item_0"] => int(0)["item_1"] => int(0)["item_2"] => int(0)["item_3"] => int(0)["item_4"] => int(0)["item_5"] => int(0)["kills"] => int(0)["deaths"] => int(0)["assists"] => int(0)["leaver_status"] => int(1)["gold"] => int(0)["last_hits"] => int(0)["denies"] => int(0)["gold_per_min"] => int(1799)["xp_per_min"] => int(0)["gold_spent"] => int(0)["hero_damage"] => int(0)["tower_damage"] => int(0)["hero_healing"] => int(0)["level"] => int(0)
}[3] => array(23) {
["account_id"] => int(4294967295)["player_slot"] => int(3)["hero_id"] => int(0)["item_0"] => int(0)["item_1"] => int(0)["item_2"] => int(0)["item_3"] => int(0)["item_4"] => int(0)["item_5"] => int(0)["kills"] => int(0)["deaths"] => int(0)["assists"] => int(0)["leaver_status"] => int(1)["gold"] => int(0)["last_hits"] => int(0)["denies"] => int(0)["gold_per_min"] => int(1799)["xp_per_min"] => int(0)["gold_spent"] => int(0)["hero_damage"] => int(0)["tower_damage"] => int(0)["hero_healing"] => int(0)["level"] => int(0)
}[4] => array(23) {
["account_id"] => int(126161349)["player_slot"] => int(4)["hero_id"] => int(0)["item_0"] => int(0)["item_1"] => int(0)["item_2"] => int(0)["item_3"] => int(0)["item_4"] => int(0)["item_5"] => int(0)["kills"] => int(0)["deaths"] => int(0)["assists"] => int(0)["leaver_status"] => int(1)["gold"] => int(0)["last_hits"] => int(0)["denies"] => int(0)["gold_per_min"] => int(1799)["xp_per_min"] => int(0)["gold_spent"] => int(0)["hero_damage"] => int(0)["tower_damage"] => int(0)["hero_healing"] => int(0)["level"] => int(0)
}[5] => array(23) {
["account_id"] => int(4294967295)["player_slot"] => int(128)["hero_id"] => int(0)["item_0"] => int(0)["item_1"] => int(0)["item_2"] => int(0)["item_3"] => int(0)["item_4"] => int(0)["item_5"] => int(0)["kills"] => int(0)["deaths"] => int(0)["assists"] => int(0)["leaver_status"] => int(1)["gold"] => int(0)["last_hits"] => int(0)["denies"] => int(0)["gold_per_min"] => int(2500)["xp_per_min"] => int(0)["gold_spent"] => int(0)["hero_damage"] => int(0)["tower_damage"] => int(0)["hero_healing"] => int(0)["level"] => int(0)
}[6] => array(23) {
["account_id"] => int(4294967295)["player_slot"] => int(129)["hero_id"] => int(0)["item_0"] => int(0)["item_1"] => int(0)["item_2"] => int(0)["item_3"] => int(0)["item_4"] => int(0)["item_5"] => int(0)["kills"] => int(0)["deaths"] => int(0)["assists"] => int(0)["leaver_status"] => int(3)["gold"] => int(0)["last_hits"] => int(0)["denies"] => int(0)["gold_per_min"] => int(1799)["xp_per_min"] => int(0)["gold_spent"] => int(0)["hero_damage"] => int(0)["tower_damage"] => int(0)["hero_healing"] => int(0)["level"] => int(0)
}[7] => array(23) {
["account_id"] => int(4294967295)["player_slot"] => int(130)["hero_id"] => int(0)["item_0"] => int(0)["item_1"] => int(0)["item_2"] => int(0)["item_3"] => int(0)["item_4"] => int(0)["item_5"] => int(0)["kills"] => int(0)["deaths"] => int(0)["assists"] => int(0)["leaver_status"] => int(1)["gold"] => int(0)["last_hits"] => int(0)["denies"] => int(0)["gold_per_min"] => int(2500)["xp_per_min"] => int(0)["gold_spent"] => int(0)["hero_damage"] => int(0)["tower_damage"] => int(0)["hero_healing"] => int(0)["level"] => int(0)
}[8] => array(23) {
["account_id"] => int(4294967295)["player_slot"] => int(131)["hero_id"] => int(0)["item_0"] => int(0)["item_1"] => int(0)["item_2"] => int(0)["item_3"] => int(0)["item_4"] => int(0)["item_5"] => int(0)["kills"] => int(0)["deaths"] => int(0)["assists"] => int(0)["leaver_status"] => int(1)["gold"] => int(0)["last_hits"] => int(0)["denies"] => int(0)["gold_per_min"] => int(2500)["xp_per_min"] => int(0)["gold_spent"] => int(0)["hero_damage"] => int(0)["tower_damage"] => int(0)["hero_healing"] => int(0)["level"] => int(0)
}[9] => array(23) {
["account_id"] => int(113794304)["player_slot"] => int(132)["hero_id"] => int(0)["item_0"] => int(0)["item_1"] => int(0)["item_2"] => int(0)["item_3"] => int(0)["item_4"] => int(0)["item_5"] => int(0)["kills"] => int(0)["deaths"] => int(0)["assists"] => int(0)["leaver_status"] => int(1)["gold"] => int(0)["last_hits"] => int(0)["denies"] => int(0)["gold_per_min"] => int(2500)["xp_per_min"] => int(0)["gold_spent"] => int(0)["hero_damage"] => int(0)["tower_damage"] => int(0)["hero_healing"] => int(0)["level"] => int(0)
}
}
我想要实现的是为具有特定account_id
的阵列接地,以获得该玩家的杀戮,死亡和助攻的值。这就是我试过的:
function searchForPlayer($id, $array) {
foreach ($array as $key => $val) {
if ($val['account_id'] === $id) {
return $key;
}
}
return null;
}
$player = searchForPlayer($accountid, $players);
但是每次都返回null。我是php的新手,所以如果我做错了什么就不会感到惊讶。谢谢!
答案 0 :(得分:1)
function searchForPlayer($id, $array) {
foreach ($array as $key => $val) {
if ($val['account_id'] == $id) {
return $key;
}
}
return null;
}
编辑:基于编辑数组结构,可以为主数组中的每个元素调用方法。
function mainSearchForPlayer($id, $mainArray) {
foreach($array as $k => $val) {
$result = searchForPlayer($id, $val);
if($result) break;
}
return $result;
}
然后致电:
$player = mainSearchForPlayer($accountid, $array);
===在php中按类型比较,这意味着如果你通过' 1'你的数组中的id是1,永远不等于
否则你可以通过这种方式检查类型:
var_dump($accountid);
如果这是一个字符串值,则可以解析为int:
$accountid = intval($accountid)
答案 1 :(得分:0)
试试这个递归函数:
function array_searchRecursive($needle, $haystack, $path=array()) {
foreach( $haystack as $key => $val ) {
if( is_array($val) && $sub = array_searchRecursive($needle, $val, $path)) {
$path = array_merge($path, array($key), $sub);
return $path;
} elseif($val == $needle) {
$path[] = $key;
return $path;
}
}
return false;
}