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.

2
  • Event when its empty you are getting inside the if? btw: there are some changes on the results empty() shows since php 5.4 php.net/manual/en/function.empty.php Commented Apr 5, 2012 at 1:22
  • yes, I have re edited the question. Commented Apr 5, 2012 at 1:52

2 Answers 2

3

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

if (count($friendActivity) > 0) {
   ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have re-edited the question, I am not trying to count the array.
0

You can try this solution:

if (sizeof($friendActivity) > 0) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.