Sort an array by positioning the elements true as first and null as last in the list

0

I have the following problem, I have an array of 4 positions, in which the user can add data, delete or edit this order, so I need to order the null fields in the last positions when there is some change when removing

Each true means that there is a number saved in that field, in this case I keep the id of another table. Example 1

POS 0 = true POS 2 = NULL
POS 3 = true
POS 4 = true

Example result 1

POS 0 = true
POS 2 = true
POS 3 = true
POS 4 = Null

foreach ($lista_espera as $key => $data) {

echo "POSICION $key:  "; echo "ESTADO : "   ;
if ($data->posicion==true ) {
echo "Existen un dato";
}

else {

echo "Es Null";

}

echo"\n";

}

With the code that I show solon I detect which position is true or null

    
asked by Jcastillovnz 28.12.2018 в 16:11
source

1 answer

0

Very easy, with the native function of php arsort . Example:

$fruits = array("d" => true, "a" => null, "b" => true, "c" => true);
arsort($fruits);

Result:

array (size=4)
  'b' => boolean true
  'd' => boolean true
  'c' => boolean true
  'a' => null

Greetings

    
answered by 28.12.2018 в 16:43