0

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

3
  • I showed the expected output. Look at carefully, the sample array are not the same on the key ET value. Commented Mar 1, 2019 at 6:34
  • in 505 array MT has value 'MT' => 25 so condition should be false right? or just value of ET and SC has to check Commented Mar 1, 2019 at 6:47
  • Yes. Correct. The problem is : it is not true when it is 26 or more. Commented Mar 1, 2019 at 6:49

1 Answer 1

1
$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

Sign up to request clarification or add additional context in comments.

6 Comments

Still not work. Both condition must be true because ET is 26 in the second array.
Since you overwrite the condition, your code does not simply work.
if min SC is 23 and min of ET is 26 then condition is true. is it? Are you trying to say SC >= 23 and ET >= 26 then true, rest all values doesn't matter?
Not only ET, for other value of KEY like EL, MT, MO, min value is 26. Just for SC min value is 23. So if SC does not meet 23, the condition must be false regardless of other value. And if any one of the value of other falls under 26, the condition must be false regardless of SC value becoming true.
Whatever criteria you explained, my snippet is working fine. Please check carefully.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.