Skip to content Skip to sidebar Skip to footer

PHP check element in multidimensional array

Reading Time: < 1 minute

If you want to check elements if exist or not in the multidimensional array using php.So now I’ll describes how to check elements is exist or not in the multidimensional array.Simple way.Look at this function.

I wrote the function to check this.You have to call only to this function for check that element.
Function

function check_in_array($findMe, $collectionArr, $data = true) {
    foreach ($collectionArr as $item) {
        if (($data ? $item === $findMe : $item == $findMe) || (is_array($item) && check_in_array($findMe, $item, $data))) {
            return true;
        }
    }
    return false;
}

This is my array.

  $myArrCol = array(array("Firefox", "IE", "Opera"), array("iPhone", "Apple"));

How to call to this function for check the element.Here I’ll check “iPhone” is exist or not.(Example-1)

  //Example-1
  echo check_in_array("iPhone", $myArrCol) ? 'iPhone Found' : 'iPhone Not found';
//Example-2
  echo check_in_array("Android", $myArrCol) ? 'Android Found' : 'Android Not found';