0
array (size=9)
 0 => 
  array (size=2)
   'x' => int 1
   'y' => int 4
  1 => 
 array (size=2)
  'x' => int 1
  'y' => int 5
  2 => 
 array (size=2)
  'x' => int 1
  'y' => int 6
  3 => 
 array (size=2)
  'x' => int 1
  'y' => int 7
 4 => 
array (size=2)
  'x' => int 1
  'y' => int 8
 5 => 
 array (size=2)
  'x' => int 4
  'y' => int 9
 6 => 
array (size=2)
  'x' => int 5
  'y' => int 9
 7 => 
array (size=2)
  'x' => int 6
  'y' => int 9
 8 => 
 array (size=2)
  'x' => int 7
  'y' => int 9

The above is an array of x and y coordinates where I have battle ships plotted, I want to search the array and return true when the x,y I send matches the x,y pair of arrays. I can do it if I choose just x on it's own, or y on it's own, but having trouble searching both x and y.

function searcharray($value, $key, $array) {
    foreach ($array as $k => $val) {
        //echo $val[$key];
        if ($val[$key] == $value) {
            return TRUE;
            //$val[$key];
        }
    }
    return null;
 }

$array;
// both these must match    

$key='y';
$value=9;
$key2='x';
Varlue2='5';


$result = searcharray($value,$key,$array);

echo $result;
ôkio
  • 1,772
  • 1
  • 15
  • 16
GAV
  • 1,205
  • 2
  • 18
  • 38

3 Answers3

1

You can calculate an intersection of 2 arrays. If they have a common element, the result would be true, otherwise false. However, because you are comparing multidimensional arrays, you need to use custom function for comparison thus the intersection should be calculated using array_uintersect():

$haystack = [['x'=>'1','y'=>'1'],['x'=>'2','y'=>'1']];
$search = [['x'=>'1','y'=>'1']]; // NOTE: this must be a multidimensional array

$result = (bool)array_uintersect($haystack, $search, function($a, $b) {
    return strcasecmp($a['x'].$a['y'], $b['x'].$b['y']);
});

TEST

tmt
  • 7,611
  • 4
  • 32
  • 46
  • Thanks, this works and I think it's a very elegant solution for how I think anyway. Not even come across array_unitersect() before – GAV Apr 21 '15 at 07:55
  • I'm having trouble making strcasecmp() work if my array is an integer. If I make it a string is works, as x and y are integers it's hard to keep forcing them to strings in one case not possible. Any ideas? – GAV Apr 21 '15 at 12:27
  • That is strange because with the `.` concatenation it should always be comparing strings, even if the originals are integers. Here is another [test](https://eval.in/316456). Could you try to modify the test so that it fails as in your case? – tmt Apr 21 '15 at 12:51
  • realised as it's a multidemsional array, I need to specify the search like this $search = array(array('x'=>'2','y'=>'6')); instead of $search = array('x'=>'2','y'=>'6'); it was catching anything starting with x=1 then nothing else – GAV Apr 21 '15 at 14:58
  • Correct! The two arrays must have the same structure. I should have highlighted it in my answer. – tmt Apr 21 '15 at 15:02
0
function isArrayEntrySet($array, $x, $y) {
    return (boolean) count(
        array_filter(
            $myArray,
            function ($value) use ($x $y) {
                return $value['x'] == $x && $value['y'] == $y;
            }
        )
    ) > 0;
}

$x = 9;
$y = 5;
if (isArrayEntrySet($array, $x, $y)) {
    echo 'BANG!!!';
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
-1

There's several ways of achieving what you want, here's a small function to help you do that:

function identical_values( $arrayA , $arrayB ) {
    sort( $arrayA );
    sort( $arrayB );
    return $arrayA === $arrayB;
} 

Basically we first sort both arrays and then compare if they are equal ===
Equal arrays will return TRUE while different return FALSE.


Example:

$array1 = array("x" => "green", "red", "blue", "red");
$array2 = array("x" => "green", "red", "blue", "red");

if(identical_values( $array1 , $array2 )){
echo "arrays are equal";
}

DEMO

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268