Higeath
Higeath

Reputation: 33

Illegal offset type when checking if array is set / empty / is array PHP

Below is an example of my array and in my code I'm trying to check if $champions (name of this whole array) isset$champions[$champion]['General'] and I get this error I need to check it since General array won't always exist

array (size=1)
  'Aatrox' => 
    array (size=1)
      'General' => 
        array (size=2)
          'Change' => 
            array (size=2)
              0 => string 'test.' (length=5)
              1 => string 'TEZd' (length=4)
          'Type' => 
            array (size=2)
              0 => string 'buff' (length=4)
              1 => string 'buff' (length=4)


foreach($champions as $champion){
        if(isset($champions[$champion]['General'])){
              ..... 
        }

}

Upvotes: 0

Views: 246

Answers (1)

William_Wilson
William_Wilson

Reputation: 1264

foreach($champions as $key => $champion){
    if(isset($champions[$key]['General'])){
          ..... 
    }

}

$champion would be the array containing the key 'General' if you want to access the array by index then you need to use the key instead.

Your other option would be isset($champion['General'])

Upvotes: 3

Related Questions