Checking if the array in foreach loop is empty or not

Go To StackoverFlow.com

1

I am trying to check if my array is empty or not but strangely I can't get it working.

foreach ($friend as $key => $id){

    $friendActivity = $db->friendActivity($id);

    if (!empty($friendActivity)) {

        //rest of the code

    }

}

I have used var_dump($friendActivity) before the if statement and the result is as follow:

array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { }  

//////---> Something to know <--------/////////

I am not trying to see the count of arrays. Each user in database has some activities. I want to see if those activities match with the (poll of of activities). So if the user has mentioned about their activities, therefore the $friendActivity is not going to be empty.

If I use empty:

if (empty($friendActivity)){

    //rest of the code

}

I am able to get inside the if condition...

I personally think, that I have to delete the array at the end of each foreach loop, so that I don't store the other user's friends activities.

2012-04-05 01:14
by Sokhrat Sikar
Event when its empty you are getting inside the if? btw: there are some changes on the results empty() shows since php 5.4 http://php.net/manual/en/function.empty.ph - dimirc 2012-04-05 01:22
yes, I have re edited the question - Sokhrat Sikar 2012-04-05 01:52


3

count() should give you an accurate array number: http://php.net/manual/en/function.count.php

if (count($friendActivity) > 0) {
   ...
}
2012-04-05 01:19
by Jonathan LeBlanc
empty should work as well. I think there is another problem.. - binarious 2012-04-05 01:22
I have re-edited the question, I am not trying to count the array - Sokhrat Sikar 2012-04-05 01:51


0

You can try this solution:

if (sizeof($friendActivity) > 0) {
2016-08-04 13:11
by Paweł
Ads