2

I want to make condition out of the following array, but it does not give my expected result because it does not run the second condition.

$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:26
     ];
     return $arr;
   },
   []
 );

The print_r($total) output the following:

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

My expected output is the following:

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

You can clearly see that I want to check: When it is 'SC' I want the minimum condition to be 23. But for others, I want the minimum value to be 26. So I used array_search() to scan the array when the element is 'SC'. Unluckily, it only checks for 'SC' and not others. Please help me.

  • Currently your code does exactly what is written - "SC" exist in both array so the minimum value is 23 for all array. Do you want it to be 26 with exception of 23 only for SC? – dWinder Feb 28 '19 at 22:49
  • @dWinder, your answer still not work. Please check my comment belowe your answer. – the thinker Mar 01 '19 at 03:59

1 Answers1

1

If I understand correctly you want the allow value for "SC" key to be minimum if 23 and for all the rest 26.

Notice that the line: array_search('SC', $sc) ? min($consumed) >= 23:26 first check if "SC" exist and set the minimum for the entire array as 23 or 26.

If you want to have different min value according to keys I would recommend this:

function checkMin($arr, $min, $exception) {
    foreach($arr as $k => $v) {
        if ($v < (isset($exception[$k]) ? $exception[$k] : $min)) 
            return false;
    }
    return true;
}

Now you can call is with:

"condition" =>  checkMin($consumed, 26, ["SC" => 23])

Hope that helps!

dWinder
  • 11,597
  • 3
  • 24
  • 39
  • Thanks. it still does not work. If min value of the key is less than 23, I want to output 1, but the min value for other key is 26. So if 'SC'=23 and ET=26, it will output 1 for it meets the condition. But SC=23, ET=25 does not meet the condition because ET min must be 26. – the thinker Mar 01 '19 at 03:40
  • @LalhriatpuiiChhunthang I don't understand - please check live example https://3v4l.org/QXaj0 - it is like your expected output. Can you please elaborate what I miss? – dWinder Mar 01 '19 at 06:48
  • Why? you have `'MT' => 25` which is less then 26 – dWinder Mar 01 '19 at 06:53
  • For keys other than SC, the minimum value is 26, and if the ET or other key exlcluding (SC which has minimum 23) is less than 26, condition must be false. – the thinker Mar 01 '19 at 06:58
  • Ok, that exactly what my function does... That why in the example you add in the comment my code return false as the condition not met for `MT` is 25 which is less then 26... (You wrote the expected output is true, why?) – dWinder Mar 01 '19 at 07:10
  • For SC, the min value must be 23 and the condition must return true. But if the value for other key like ET, EL, MT, MO is less than 26, then it must return false regardless of SC (which is more than or equal to 23). In other case, If the value of the keys ET,EL,MO,MT are all more than or equal to 26 and if SC value is less than 23, it will return false. Is it clear enough. Thanks for the help. – the thinker Mar 01 '19 at 07:33