简体   繁体   中英

how to check if an array is null

i am parsing a xml api using curl. the curl provides an array and i fetch it using foreach loop but most of the time i get array like below which is not fetched by foreach loop. the array is like below

  array(
     [0]=>
      )

so how to check if the array is null so that the foreach has not to fetch the null array. i mean which (if()) condition should i use.

the another problem is that why curl gives empty array most of the time. what is the problem. i am working on localhost and the api is called from remote server. please help me.

An array cannot be null. A variable can be null if no value is assigned to it, but when you create an empty array and assign it to a variable, the value of that variable is an array - not null.

The array, on the other hand can be empty or have the size of zero. So using empty() or count() would give you what you are looking for.

To check if an array is null, use the empty() function. It returns false if array is empty.

empty()

ie

$test = "";

if (empty($test)) { echo "true" }

would return true

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