RuleBritannia Posted December 16, 2013 Share Posted December 16, 2013 Array 0 ( [width] => 400 [height] => 300 [start] => 200 [end] => 100 ) Array 1 ( [width] => 40 [height] => 30 [start] => 20 [end] => 10 ) Say I have these 2 arrays, I want to check if array 0 values are all less than array 1. Is there a function to do this? Or possibly use a function specified for something else to achieve what we want? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/284802-best-way-to-check-if-one-array-values-less-than-another-array/ Share on other sites More sharing options...
Ch0cu3r Posted December 16, 2013 Share Posted December 16, 2013 Could try array_diff_assoc $array1 = array( 'width' => 400, 'height' => 300, 'start' => 200, 'end' => 100 ); $array2 = array( 'width' => 400, 'height' => 30, 'start' => 20, 'end' => 10 ); $diff = array_diff_assoc($array2, $array1); printf('Values that are lower in $array2 are: <pre>%s</pre>', print_r($diff, true)); Quote Link to comment https://forums.phpfreaks.com/topic/284802-best-way-to-check-if-one-array-values-less-than-another-array/#findComment-1462470 Share on other sites More sharing options...
RuleBritannia Posted December 16, 2013 Author Share Posted December 16, 2013 (edited) That wont work as array_diff_assoc checks if each value is equal(===) to one another. So if we set width in array2 to 40000, It will be returned as a lower amount, When it isn't. Thanks anyway though. Edited December 16, 2013 by RuleBritannia Quote Link to comment https://forums.phpfreaks.com/topic/284802-best-way-to-check-if-one-array-values-less-than-another-array/#findComment-1462471 Share on other sites More sharing options...
Barand Posted December 16, 2013 Share Posted December 16, 2013 Try $array1 = array( 'width' => 400, 'height' => 300, 'start' => 10, 'end' => 100 ); $array2 = array( 'width' => 400, 'height' => 30, 'start' => 20, 'end' => 10 ); foreach ($array1 as $k => $v) { if ($array2[$k] >= $v) { unset($array2[$k]); } } echo '<pre>',print_r($array2, true),'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/284802-best-way-to-check-if-one-array-values-less-than-another-array/#findComment-1462472 Share on other sites More sharing options...
RuleBritannia Posted December 16, 2013 Author Share Posted December 16, 2013 Came up with this, still not elegant enough for me to stop thinking thought $array1 = array( 'width' => 400, 'height' => 300, 'start' => 200, 'end' => 10 ); $array2 = array( 'width' => 4000, 'height' => 30, 'start' => 20, 'end' => 100 ); function c($a,$b) { if($a <= $b) { return 0; } elseif($a > $b) { return 1; } } print_r(array_uintersect_assoc($array1,$array2,'c')); Quote Link to comment https://forums.phpfreaks.com/topic/284802-best-way-to-check-if-one-array-values-less-than-another-array/#findComment-1462474 Share on other sites More sharing options...
Solution scootstah Posted December 16, 2013 Solution Share Posted December 16, 2013 I'm not 100% clear on what you're looking for. Your original wording was that you "want to check if array 0 values are all less than array 1." So, this should do that: $array1 = array( 'width' => 400, 'height' => 300, 'start' => 200, 'end' => 10 ); $array2 = array( 'width' => 4000, 'height' => 30, 'start' => 20, 'end' => 100 ); function aLessThanB($a, $b) { foreach($a as $key => $val) { if ($val > $b[$key]) { return false; } } return true; }This will return true if all elements of array1 are < the corresponding elements of array2, otherwise it will return false. Part of me also thinks that perhaps you want to retrieve the lower value for each element between array1 and array2. So, this will do that: $array1 = array( 'width' => 400, 'height' => 300, 'start' => 200, 'end' => 10 ); $array2 = array( 'width' => 4000, 'height' => 30, 'start' => 20, 'end' => 100 ); function getLowestValues($a, $b) { $c = array(); foreach($a as $key => $val) { $c[$key] = min($val, $b[$key]); } return $c; } Returns: Array ( [width] => 400 [height] => 30 [start] => 20 [end] => 10 ) Quote Link to comment https://forums.phpfreaks.com/topic/284802-best-way-to-check-if-one-array-values-less-than-another-array/#findComment-1462475 Share on other sites More sharing options...
Barand Posted December 16, 2013 Share Posted December 16, 2013 function c($a,$b) { if($a <= $b) { return 0; } elseif($a > $b) { return 1; } } You could simplify that to function c($a,$b) { return ($a > $b); } Quote Link to comment https://forums.phpfreaks.com/topic/284802-best-way-to-check-if-one-array-values-less-than-another-array/#findComment-1462477 Share on other sites More sharing options...
.josh Posted December 16, 2013 Share Posted December 16, 2013 use an anonymous function if you really wanna go for cramming 1-lining it: print_r(array_uintersect_assoc($array1,$array2,function($a,$b){return $a>$b;})); Quote Link to comment https://forums.phpfreaks.com/topic/284802-best-way-to-check-if-one-array-values-less-than-another-array/#findComment-1462479 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.