Search in Array with conditions

Asked

Viewed 70 times

0

I have a Array of records:

Array
(
    [0] => Array
        (
            [HANDLE] => 1
            [Z_GRUPO] => 
            [EMPRESA] => 1
            [FILIAL] => 1
            [DATAINICIO] => 2018-01-01 
            [CONSUMO] => 2
            [VEICULO] => 82
        )

    [1] => Array
        (
            [HANDLE] => 2
            [Z_GRUPO] => 
            [EMPRESA] => 1
            [FILIAL] => 1
            [DATAINICIO] => 2018-02-01
            [CONSUMO] => 2.5
            [VEICULO] => 82
        )

    [2] => Array
        (
            [HANDLE] => 3
            [Z_GRUPO] => 
            [EMPRESA] => 1
            [FILIAL] => 1
            [DATAINICIO] => 2018-03-01
            [CONSUMO] => 3
            [VEICULO] => 82
        )

    [3] => Array
        (
            [HANDLE] => 4
            [Z_GRUPO] => 
            [EMPRESA] => 1
            [FILIAL] => 1
            [DATAINICIO] => 2018-02-01
            [CONSUMO] => 3.5
            [VEICULO] => 102
        )

)

Question:

Would like to search in the Array having as reference a DATE AND A VEHICLE.


Example:

What is the CONSUMPTION of VEICULO 82 in DATA 10/02/2018?

If it were a database, it would simply be this:

SELECT TOP 1 * FROM VEICULOCONSUMO
WHERE VEICULO = 82
AND DATAINICIO <= '10/02/2018'
ORDER BY DATAINICIO DESC

The correct return would be: 2.5

How the record was made in 10/02/2018, then the most recent or equal previous date was [1][DATAINICIO] => 2018-02-01.

The "X" of the question is how to pull the CONSUMPTION referring to DATE and VEHICLE, where the DATE equals START OR LATEST DATE.

  • DATAINICIO is a string ? This data comes from where, from the database ? If it is the case it is not easier to bring soon what you want, with the query that exemplified ?

  • @Isac the date comes in this format that I’m finding strange sqlsrv_fetch_array returns timedate field as object... It comes from a BD but I bring other related records, so there is no way to make association in the query...

1 answer

1


You can pick up the records that have vehicle equal to 82 and start date less or equal to 2018-02-10 with the function array_filter:

$filtrados = array_filter($registros, function ($registro) {
    return ($registro['VEICULO'] == 82) and ($registro['DATAINICIO'] <= '2018-02-10');
});

Sort result by start date, from highest to lowest:

usort($filtrados, function ($a, $b) {
    return $a['DATAINICIO'] <=> $b['DATAINICIO'];
});

And, as you just want a record, take the last array value:

$final = array_pop($filtrados);
print_r($final);

Staying:

Array
(
    [HANDLE] => 2
    [Z_GRUPO] => 1
    [EMPRESA] => 1
    [FILIAL] => 1
    [DATAINICIO] => 2018-02-01
    [CONSUMO] => 2.5
    [VEICULO] => 82
)

Which is the most recent entry, with date less than or equal to 2018-02-10.

  • Woss, can not add up. It would be like this: on date 10/02/2018 a document has been made by the vehicle 82, then I want to know what was the consumption of that vehicle that day that this document was made (in this case, this is an average consumption km/L).

  • It’s kind of hard to understand! rs... so I even pasted the example of select which is exactly what I wanted to do in the array... I need to pick up the consumption on 10/02/2018 OR the most recent previous...

  • I don’t understand here: array_filter($registros, function ($registro) {... $registros is my Array correct!? And the $registro would be what?

  • @RBZ Each item in your array...

  • Perfect, great too man! ... just there usort who is $B (capital letters) and calling $b (tiny)... and the comparison is <=>, worked with <=.

  • The <=> operator is the Spaceship, created precisely for this.

  • But he makes a mistake Parse error: syntax error, unexpected '>' in

  • PHP version, then.

  • So it’s probably just the 7. I’m noticing here, and the usort organized right, but the array_pop picks up the last one, then picks up the wrong one. I’ll look at how they work to hit...

  • As defined in PHP 5 ... return $a['DATAINICIO'] >= $b['DATAINICIO'];

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.