Question

I have this array (shortened for readability)

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

What i'm trying to achieve is earthing for the array with a specific account_id in order to get the values for kills, deaths and assists for that player. This is what I tried:

function searchForPlayer($id, $array) {
    foreach ($array as $key => $val) {
        if ($val['account_id'] === $id) {
            return $key;
        }
    }
    return null;
}

$player = searchForPlayer($accountid, $players);

However it returns null every time. I'm new to php so would not be surprised if I'm doing something totally wrong. Thanks!

Was it helpful?

Solution

function searchForPlayer($id, $array) {
    foreach ($array as $key => $val) {
        if ($val['account_id'] == $id) {
           return $key;
        }
    }
    return null;
}

EDIT: Based in you edit your array structure, you can call the method for each element in the main array.

function mainSearchForPlayer($id, $mainArray) {
    foreach($array as $k => $val) {
      $result = searchForPlayer($id, $val);
      if($result) break;
    }
    return $result;
}

And then call:

$player = mainSearchForPlayer($accountid, $array);

=== in php compare by type too this mean if you pass '1' and your id in array is 1 , never be equals

otherwise you can check the types by this way:

var_dump($accountid);

and if this is a string value , you can parse to int:

$accountid = intval($accountid)

OTHER TIPS

Try this recursive function:

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top