Reputation: 2659
I can't find out how to check if an array is empty. I know empty()
means 100% empty, including keys. But my array looks like this when there are (in this case) no products:
Array
(
[0] =>
)
How can I check if an array is empty like that? Preferably only for this exact "array list" because on a page that does have products I also have [0] =>
as first value, which I filter out (but this is after I need to check for the empty array).
Edit:
if(empty(array_values($relatedcr))){
echo 'empty';
}else{
echo 'not empty';
}
Upvotes: 1
Views: 46
Reputation: 375
You can do it using array_filter then check for empty
$b = array_filter($array1);
if (empty($b))
{
echo "empty";
}
else
{
echo "not empty";
}
Upvotes: 0
Reputation: 28529
get the value as array, then check it.
empty(array_values($array));
Here is a test code:
<?php
$array=[1];
unset($array[0]);
var_dump($array);
var_dump(empty($array));
var_dump(['']);
var_dump(empty(['']));
output: demo here
array(0) {
}
bool(true)
array(1) {
[0]=>
string(0) ""
}
bool(false)
Upvotes: 3
Reputation: 2215
<?php
$user= [
"name"=> "",
"age" => ""
];
$data = array_filter($user);
echo (empty($data)) ? "empty" : "not empty";
output : empty
Upvotes: 0
Reputation: 549
This way:
foreach ($playerlist as $key => $value) {
if (empty($value)) { //checking if array value are empty
unset($playerlist[$key]);
}
}
Upvotes: 0