How to get the minimum value from array row

前端 未结 6 1743
情话喂你
情话喂你 2021-01-25 01:15

I trying to get the minimum values from the any column contains \"xx\" in the column name.

Below is my code:



        
6条回答
  •  长情又很酷
    2021-01-25 02:17

    1. Iterate each row/subarray with a foreach() loop or array_walk().
    2. Extract and display the id (first element) value with array_shift().
    3. Call min() on the remaining values in the respective subarray to determine the lowest value.

    No conditional expressions. No unnecessary variables. Clean, concise, and effective.

    Code: (Demo)

    $array = [
        ['id' => 1, '10xx' => 14, '11xx' => 32, '12xx' => 4],
        ['id' => 2, '10xx' => 13, '11xx' => 36, '12xx' => 41]
    ];
    
    array_walk($array, function($row) {
        echo array_shift($row) , " : " , min($row) , "\n";
    });
    

    Output:

    1 : 4
    2 : 13
    

提交回复
热议问题