Jump to content

Best way to check if one array values less than another array?


RuleBritannia

Recommended Posts

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.

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));

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>';

Came up with this, still not elegant enough for me to stop thinking thought :hammer_time::confused:

$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'));

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
)

 


    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);
    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.