2

I am using one custom CMS developed by someone and getting issue to check the array result.

The function returns an array of username by passing userids array.

Example Code

$all_users = "1,5,9,10,25,40"; // getting from database

$user_ids = explode(',', $all_users);
$usernames  = get_userids_to_usernames($user_ids); //this returns usernames array by passing uesrids array

If the database has not users in the column than the function is returning weird empty / null array as below.

var_dump($usernames);

array(1) { [""]=> NULL }

print_r($usernames);

(
    [] => 
)

Now issue is, I want to check if array is empty or has value in return but I have tried everything is_null, empty, count($usernames) > 0 but none of these working.

Can anyone please help me to check conditionally if array has value or empty like above empty result.

9
  • 1
    That's not empty though. Can you just test if there is a single item, with key "" and a value of null? 3-4 conditions (is_array, count, isset, ==(=)), one if. Commented Feb 1, 2019 at 13:06
  • use is_null() to check if value is null Commented Feb 1, 2019 at 13:10
  • @splash58 yes I have tested all these and I am getting same false result. Any idea how to check it so can use conditional statement for such result? Commented Feb 1, 2019 at 13:10
  • @niklaz is_null not working as well. Commented Feb 1, 2019 at 13:10
  • 1
    "1,5,9,10,25,40"; // getting from database why would a database return CSV ID's? Commented Feb 1, 2019 at 13:12

4 Answers 4

3

Here is a workaround

if (array_key_exists('', $a) && $a[''] === null) {
    unset($a['']);
}

then check on emptiness

Sign up to request clarification or add additional context in comments.

1 Comment

This looks like the safest of the suggested solutions. With a custom made CMS you can't expect that it will always behave the same, and this answer takes that into account.
1

You can use in_array to check if the array has an empty value and array_key_exists to check the key.

in_array("", $array)
array_key_exists("", $array)

http://php.net/manual/en/function.in-array.php

http://php.net/manual/fr/function.array-key-exists.php

6 Comments

This seems working but would test with other answers too. Just to find best solution.
@CodeLover and how does it work if in_array search value but '' is key ?
Check does the array have element which value is an empty string by making php cast types? It's the best way to end with a pile of bugs
@splash58 the biggest issue here is by checking empty or count doesn't works here as it is returning false So I believe @Dialex solutions works (I have tested) and @line88 solution works too. Now need to see which is better.
@splash58 @Code Lover You can use array_key_exists to check for empty keys as well. I just forgot that you were looking for that. You can maybe use them combined in a if like that: if(in_array("", $array) || array_key_exists("", $array))
|
0

Iterate through array, and check if keys or values are empty or null

$newarray = [];
foreach($array as $key=>$value)
{
    if(is_null($value) || trim($value) == '' || is_null($value) || trim($key) == ''){
    continue; //skip the item
  }
        $newarray[$key] = $value;
}

Comments

0

If you want to use empty with it, try array filter

if (empty(array_filter($arr))) {
    //Do something
}

Array filter will automatically remove falsey values for you, and you can include a callback should you need more flexability.

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.