Jump to content

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


RuleBritannia
Go to solution Solved by scootstah,

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.

Link to comment
Share on other sites

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'));
Link to comment
Share on other sites

  • Solution

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
)
Link to comment
Share on other sites

 


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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.