简体   繁体   中英

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'])){
              ..... 
        }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM