Validate values of an array in php

1

What I need is to validate that at least one position of an array is NOT NULL. For example, I have an array that contains search criteria, that after a process, becomes NULL or with a search value, then I need to validate (before sending to process) that the array has at least 1 value in some position, and that is not all NULL:

    $criterios = array(
    "criterio1" => "",
    "criterio2" => "",
    "criterio3" => "",
    "criterio4" => "",
    "criterio5" => "",
    "criterio6" => "",
    "criterio7" => "",
);

I found a function to determine if a value exists within an array.

$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
 echo "Existe Irix";
}

Source: link

And also a function that searches within an associative array.

$array = array(0 => 'azul', 1 => 'rojo', 2 => 'verde', 3 => 'rojo');
$clave = array_search('verde', $array); // $clave = 2;
$clave = array_search('rojo', $array);  // $clave = 1;

source: link

Is there a function for this and if not? what would be the best way to validate it?

Note: It is essential to take into account "the best way" since there are really many search criteria and validations with "if" would be very "brusque".

Thanks in advance. A hug.

    
asked by Neftali Acosta 12.05.2018 в 10:44
source

3 answers

1

:( One apology to all, I think that the fatigue blocks me, well I have discovered this possible solution, can someone recommend me something better?

$band=false;
foreach ($criterios as $value) {
    if ($value!==NULL){
        $band = true;
        break;
    }
}
    
answered by 12.05.2018 / 11:13
source
2

Seeing that the array is all empty strings you could do a array_filter() and then check if there is any element, also you will get an array only with the keys that have some value keeping the keys.

Example:

    $criterios = array(
    "criterio1" => "",
    "criterio2" => "",
    "criterio3" => "",
    "criterio4" => "",
    "criterio5" => "",
    "criterio6" => "",
    "criterio7" => "",
);

// Pasamos array_filter y asignamos a $el lo devuelto
// comprobamos con el condicional si el Array esta vacio
if ($el = array_filter($criterios)) {
    echo 'Hay criterios';
    // imprimimos los valores devueltos por array_filter
    print_r($el);
} else {
    echo 'No hay criterios";
}

Documentation array_filter() : link

    
answered by 12.05.2018 в 12:41
1

Depending on whether the empty strings count as "value", that is, if you only want it not to be all NULL you can use implode , array_filter or array_filter with condition.

Here are some examples of each:

  • case 1: an item has value the rest are empty strings or NULL
  • case 2: one item is NULL the rest are empty strings
  • case 3: all items are 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;

the results (3 cases, 3 forms of 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
    
answered by 12.05.2018 в 13:20