-1

I have an array called all_teams which contains the following

Array
(
    [33448] => Team1
    [33466] => Team2
    [33467] => Team3
    [33476] => Team4
    [33495] => Team5
)

I do a check within a foreach to check if teamId is in the array keys. If the array key exists I want to display the key's value.

So far I have

if(array_key_exists(intval($team['teamId']), $all_teams)) {
   echo 'set';
   // array key value needs to be here
} else {
   echo 'not set';
}
Purple Haze
  • 530
  • 7
  • 22
CIB
  • 535
  • 1
  • 12
  • 35
  • 4
    Can't you just use `$all_teams[$intval($team['teamId'])]`? You basically want to access a value based on a key/index :) – Terry Apr 03 '18 at 12:50
  • `array_key_exists($team['teamId'],$all_teams)` try with this – Bhargav Chudasama Apr 03 '18 at 12:52
  • 1
    If [`array_key_exists($team['teamId'], $all_teams)`](http://php.net/manual/en/function.array-key-exists.php) returns `TRUE` then `$all_teams[$team['teamId']]` is the value you need. Read about [PHP arrays](http://php.net/manual/en/language.types.array.php). – axiac Apr 03 '18 at 12:53
  • @Terry you nailed it! Can't believe I missed something so simple. Thanks – CIB Apr 03 '18 at 12:54

1 Answers1

1

As per my comment, you simply want to access an array element's value by its index. It is as straightforward as doing $all_teams[<<index>>], which is in this case the parsed teamId:

$teamId = intval($team['teamId']);
if(array_key_exists($teamId, $all_teams)) {
   echo $all_teams[$teamId];
} else {
   echo 'not set';
}
Terry
  • 63,248
  • 15
  • 96
  • 118