Jump to content

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


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.

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

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 by RuleBritannia

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

 


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

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.