简体   繁体   中英

Multiple Condition inside php array

How do I check the minimum value inside php array with multiple condition. My array is below:

$arr = [[472 =>['EL' => 52.9,'MT' => 57.375 'MO' => 56.6,'SC' => 26,'ET' => 50.775]], 
       [505 =>['EL' => 53.425,'MT' => 25,'MO' => 62.8,'SC' => 23,'ET' => 25]]];    
$total = array_reduce(
   $arr,
   function($arr, $key) {
     $id = key($key);
     $consumed = $key[$id];
     $sc = array_keys($consumed);
     $arr[$id] = [
       "totalc" => array_sum($consumed),
       "condition" =>  array_search('SC', $sc) ? min($consumed) >= 23:min($consumed) >=26
     ];
     return $arr;
   },
   []
 );

If the key is 'SC', the minimum value for 'SC' is 23, But for the value of other key the minimum value is 26. So the above code must produce the following output:

 Array
(
    [472] => Array
        (
            [totalc] => 243.65
            [condition] => 1
        )    
    [505] => Array
        (
            [totalc] => 189.225
            [condition] => 
        )
    )

The SC key in both the array ( [472] and [505] ) meet the minimum condition 23 but the value of the key ET does not meet the minimum condition 26. So the array [505] should produce false on the value of condition key. But if we change the value of ET to 26 or more on the [505] array, we still get false value. But the correct value must be 1 or true . So if the array is like below:

$arr = [[472 =>['EL' => 52.9,'MT' => 57.375 'MO' => 56.6,'SC' => 26,'ET' => 50.775]], 
           [505 =>['EL' => 53.425,'MT' => 25,'MO' => 62.8,'SC' => 23,'ET' => 26]]];

The correct output would be :

Array
    (
        [472] => Array
            (
                [totalc] => 243.65
                [condition] => 1
            )    
        [505] => Array
            (
                [totalc] => 189.225
                [condition] => 1
            )
        )

In short, the min value of 'SC' should be 23 and the mim value of other KEYS(EL, MT,MO,ET) should be 26. I tried to solve this problem for almost two days and two nights, it still does not work. So if you could help me, I would really appreciate it. Thanks

$arr = [[472 => ['EL' => 52.9, 'MT' => 57.375, 'MO' => 56.6, 'SC' => 26, 'ET' => 50.775]],
        [505 => ['EL' => 53.425, 'MT' => 25, 'MO' => 62.8, 'SC' => 23, 'ET' => 26]]];
function pr($arr)
{
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}

$total = array_reduce($arr, function ($arr, $key) {
    $id   = key($key);
    $temp = $consumed = $key[$id];
    $sc   = array_keys($consumed);
    $condition = false;
    if (array_search('SC', $sc) && ($consumed['SC']) >= 23) {
        $condition = true;
    }
    unset($temp['SC']);

    $condition = ($condition && min($temp) >= 26 ? true : false);
    $arr[$id] = [
        "totalc"    => array_sum($consumed),
        "condition" => $condition,
    ];
    return $arr;
});

pr($total);die;

if min SC is 23 and min of all other is 26 then condition is true.

Explanation:

472 => SC = 26(>=23) and all other values are >= 26 so condition = true
505 => SC = 23(>=23) and now I will check other values. Now MT = 25 so condition = false

Lets take some sample demo examples,

$arr = [[472 => ['EL' => 52.9, 'MT' => 57.375, 'MO' => 56.6, 'SC' => 22, 'ET' => 50.775]],
        [505 => ['EL' => 53.425, 'MT' => 26, 'MO' => 62.8, 'SC' => 23, 'ET' => 26]]];

472 => SC < 23 so condition = false regardless of other value.
505 => SC >= 23 so condition = true at first, now we will check for all other values. As we saw all other are greater than 26, so condition =true .

Working demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM