当前位置: 动力学知识库 > 问答 > 编程问答 >

Find array values within Multidimensional array

问题描述:

<?php

function multi_array_search($search_for, $search_in) {

foreach ($search_in as $element) {

if ( ($element === $search_for) || (is_array($element) && multi_array_search($search_for, $element)) ){

return true;

}

}

return false;

}

$arr = array("2014", array("January", "February", "March"), "2015", array("Monday", "Tuesday"));

echo multi_array_search("Tuesday", $arr) ? 'Found' : 'Not found';

?>

In the above code

In multi_array_search("Tuesday",$arr);

we are passing first parameter is value and second parameter is an array,

But my query is that we are passing first parameter is a set of values.

i.e. we are taking$array=array("2015","Tuesday","March"); Like this and when we found matching values then return 'Found' otherwise return 'Not found' also the value may be case-sensitive,

网友答案:

Converting the the arguments to lowercase by using strtolower() function of PHP makes this job done. The code I come up with is as below, I hope it helps you.

<?php
function multi_array_search($search_for, $search_in) {
$search_for=strtolower($search_for);
foreach ($search_in as $element) {
  if(!is_array($element))
    $element=strtolower($element);
  if ( ($element === $search_for) || (is_array($element) && multi_array_search($search_for, $element)) ){
      return true;
  }
}
return false;
}
$arr = array("2014", array("January", "February", "March"), "2015", array("Monday", "Tuesday"));
echo multi_array_search("TUESDAY", $arr) ? 'Found' : 'Not found';
?>
分享给朋友:
您可能感兴趣的文章:
随机阅读: