簡體   English   中英

搜索多維數組PHP

[英]Searching multidimensional array PHP

我有這個數組(為便於閱讀而縮短)

   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新手,所以如果我做錯了什么也不會感到驚訝。 謝謝!

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)

嘗試以下遞歸函數:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM