Dependiendo de si las cadenas vacías cuentan como "valor", es decir si sólo buscas que no sea todo NULL
podes usar implode
, array_filter
ó array_filter
con condición.
Te dejo unos ejemplos de cada uno:
- caso 1 : un item tiene valor el resto son cadenas vacías o NULL
- caso 2 : un item es NULL el resto son cadenas vacías
- caso 3 : todos los items son NULL
arrayEmpty.php
#!/usr/bin/env php
<?php
$caso1 = array(
"criterio1" => "",
"criterio2" => "valor",
"criterio3" => "",
"criterio4" => "",
"criterio5" => NULL,
"criterio6" => "",
"criterio7" => "",
);
$caso2 = array(
"criterio1" => "",
"criterio2" => "",
"criterio3" => "",
"criterio4" => "",
"criterio5" => NULL,
"criterio6" => "",
"criterio7" => "",
);
$caso3 = array(
"criterio1" => NULL,
"criterio2" => NULL,
"criterio3" => NULL,
"criterio4" => NULL,
"criterio5" => NULL,
"criterio6" => NULL,
"criterio7" => NULL,
);
echo "----------- caso 1".PHP_EOL;
var_dump($caso1);
$usarImplode = strlen(implode("",$caso1));
echo '$usarImplode = strlen(implode("",$caso1)); -> ';
if ($usarImplode) :
echo 'SI '.$usarImplode.PHP_EOL;
else :
echo 'NO '.$usarImplode.PHP_EOL;
endif;
$usarFilter = count(array_filter($caso1));
echo '$usarFilter = count(array_filter($caso1)); -> ';
if ($usarFilter) :
echo 'SI '.$usarFilter.PHP_EOL;
else :
echo 'NO '.$usarFilter.PHP_EOL;
endif;
$usarFilter2 = count(array_filter($caso1, function($val){return $val!==NULL;}));
echo '$usarFilter2 = count(array_filter($caso1, function($val){return $val!==NULL;})); -> ';
if ($usarFilter2) :
echo 'SI '.$usarFilter2.PHP_EOL;
else :
echo 'NO '.$usarFilter2.PHP_EOL;
endif;
echo "----------- caso 2".PHP_EOL;
var_dump($caso2);
$usarImplode = strlen(implode("",$caso2));
echo '$usarImplode = strlen(implode("",$caso2)); -> ';
if ($usarImplode) :
echo 'SI '.$usarImplode.PHP_EOL;
else :
echo 'NO '.$usarImplode.PHP_EOL;
endif;
$usarFilter = count(array_filter($caso2));
echo '$usarFilter = count(array_filter($caso2)); -> ';
if ($usarFilter) :
echo 'SI '.$usarFilter.PHP_EOL;
else :
echo 'NO '.$usarFilter.PHP_EOL;
endif;
$usarFilter2 = count(array_filter($caso2, function($val){return $val!==NULL;}));
echo '$usarFilter2 = count(array_filter($caso2, function($val){return $val!==NULL;})); -> ';
if ($usarFilter2) :
echo 'SI '.$usarFilter2.PHP_EOL;
else :
echo 'NO '.$usarFilter2.PHP_EOL;
endif;
echo "----------- caso 3".PHP_EOL;
var_dump($caso3);
$usarImplode = strlen(implode("",$caso3));
echo '$usarImplode = strlen(implode("",$caso3)); -> ';
if ($usarImplode) :
echo 'SI '.$usarImplode.PHP_EOL;
else :
echo 'NO '.$usarImplode.PHP_EOL;
endif;
$usarFilter = count(array_filter($caso3));
echo '$usarFilter = count(array_filter($caso3)); -> ';
if ($usarFilter) :
echo 'SI '.$usarFilter.PHP_EOL;
else :
echo 'NO '.$usarFilter.PHP_EOL;
endif;
$usarFilter2 = count(array_filter($caso3, function($val){return $val!==NULL;}));
echo '$usarFilter2 = count(array_filter($caso3, function($val){return $val!==NULL;})); -> ';
if ($usarFilter2) :
echo 'SI '.$usarFilter2.PHP_EOL;
else :
echo 'NO '.$usarFilter2.PHP_EOL;
endif;
los resultados (3 casos, 3 formas de test)
$ ./arrayEmpty.php
----------- caso 1
array(7) {
["criterio1"]=>
string(0) ""
["criterio2"]=>
string(5) "valor"
["criterio3"]=>
string(0) ""
["criterio4"]=>
string(0) ""
["criterio5"]=>
NULL
["criterio6"]=>
string(0) ""
["criterio7"]=>
string(0) ""
}
$usarImplode = strlen(implode("",$caso1)); -> SI 5
$usarFilter = count(array_filter($caso1)); -> SI 1
$usarFilter2 = count(array_filter($caso1, function($val){return $val!==NULL;})); -> SI 6
----------- caso 2
array(7) {
["criterio1"]=>
string(0) ""
["criterio2"]=>
string(0) ""
["criterio3"]=>
string(0) ""
["criterio4"]=>
string(0) ""
["criterio5"]=>
NULL
["criterio6"]=>
string(0) ""
["criterio7"]=>
string(0) ""
}
$usarImplode = strlen(implode("",$caso2)); -> NO 0
$usarFilter = count(array_filter($caso2)); -> NO 0
$usarFilter2 = count(array_filter($caso2, function($val){return $val!==NULL;})); -> SI 6
----------- caso 3
array(7) {
["criterio1"]=>
NULL
["criterio2"]=>
NULL
["criterio3"]=>
NULL
["criterio4"]=>
NULL
["criterio5"]=>
NULL
["criterio6"]=>
NULL
["criterio7"]=>
NULL
}
$usarImplode = strlen(implode("",$caso3)); -> NO 0
$usarFilter = count(array_filter($caso3)); -> NO 0
$usarFilter2 = count(array_filter($caso3, function($val){return $val!==NULL;})); -> NO 0