PHP split a separate variable by "|" and bring all results that are not empty

0

I have a variable that is the footage of the building units and each unit is separated by "|" and it can have up to 4 units, eg 35|41|50|12 . But it does not always have 4 units, ex: 41|50||

Then I need to "break" this variable into individual variables and I thought about doing with explode:

$unidades = $row->fotos;
$unidades = explode("|", $unidades);
$un0 = $unidades[0];
$un1 = $unidades[1];
$un2 = $unidades[2];
$un3 = $unidades[3];

The problem in this case is that from there it brings the empty $ tbm photos.

How can I do to bring only those that have some value? And also get the highest and lowest value?

In summary, what I need is to get the value 35|41|50|12 for example and show the lowest and highest value, something like: echo $ lowest value. ' to the highest value.

    
asked by anonymous 08.05.2018 / 20:10

2 answers

0

You can filter the values with the array_filter () function min () and max () , for the specific values.

Ex:

$unidades = $row->fotos;
$unidades = explode("|", $unidades);

$unidades = array_filter($unidades, 'strlen');

To get the lowest value.

$menorvalor = min($unidades);

To get the highest value

$maiorvalor = max($unidades);
    
08.05.2018 / 20:18
0

Another way is to check if the values were all received so just put them in the vector

<?php
    $unit = $_GET['unit'];
    $unidades = explode("|", $unit);
    $units = array();



    foreach ($unidades as $key => $val) {

        if( !empty( $val ) ){
            echo "Unidades Rec[" . $key . "] = " . $val . "<br>";
            $units[] = $val;
        }

    }
    sort($units);


    echo "Unidades: ".$menorvalor = min($units)." a ".$maiorvalor = max($units);



?>
    
08.05.2018 / 20:55