Home  >  Article  >  Backend Development  >  PHP determines whether a certain value exists in a multidimensional array_PHP tutorial

PHP determines whether a certain value exists in a multidimensional array_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:48:202489browse

Today we will introduce to you how to determine whether the element value we are looking for exists in the array. Here, if it is one-dimensional data, you can directly in_array, but multi-dimensional data is a bit more complicated.

Let’s first solve the problem of in_array checking whether a certain value exists in the array

The code is as follows
 代码如下 复制代码

$os = array("Mac", "NT", "Irix", "Linux");

echo “(1)”;
if (in_array("Irix", $os)) {
echo "Got Irix";
}
if (in_array("mac", $os)) {//in_array() 是区分大小写的
echo "Got mac";
}

$a = array('1.10', 12.4, 1.13);
echo "(2)";

if (in_array('12.4', $a, true)) {//in_array() 严格类型检查
echo "'12.4' found with strict checkn";
}
if (in_array(1.13, $a, true)) {
echo "1.13 found with strict checkn";
}

$a = array(array('p', 'h'), array('p', 'r'), 'o');
echo "(3)";

if (in_array(array('p', 'h'), $a)) {
echo "'ph' was foundn";

}

if (in_array(array('f', 'i'), $a)) {//in_array() 中用数组作为 needle
echo "'fi' was foundn";
}
if (in_array('o', $a)) {
echo "'o' was foundn";
}
?>

 

程序运行结果是:

(1)Got Irix

(2)1.13 found with strict check

(3)'ph' was found    'o' was found

Copy code


$os = array("Mac", "NT", "Irix", "Linux");
代码如下 复制代码

$arr = array(
array('a', 'b'),
array('c', 'd')
);

in_array('a', $arr); // 此时返回的永远都是 false
deep_in_array('a', $arr); // 此时返回 true 值

function deep_in_array($value, $array) {
foreach($array as $item) {
if(!is_array($item)) {
if ($item == $value) {
return true;
} else {
continue;
}
}

if(in_array($value, $item)) {
return true;
} else if(deep_in_array($value, $item)) {
return true;
}
}
return false;
}

echo “(1)”;

if (in_array("Irix", $os)) {

echo "Got Irix";
}

if (in_array("mac", $os)) {//in_array() is case sensitive

echo "Got mac";
}

$a = array('1.10', 12.4, 1.13);<🎜> echo "(2)";<🎜> <🎜>if (in_array('12.4', $a, true)) {//in_array() strict type checking<🎜> echo "'12.4' found with strict checkn";<🎜> }<🎜> if (in_array(1.13, $a, true)) {<🎜> echo "1.13 found with strict checkn";<🎜> }<🎜> <🎜> <🎜> <🎜> <🎜>$a = array(array('p', 'h'), array('p', 'r'), 'o');<🎜> echo "(3)";<🎜> <🎜>if (in_array(array('p', 'h'), $a)) {<🎜> echo "'ph' was foundn";<🎜> <🎜>}<🎜> <🎜>if (in_array(array('f', 'i'), $a)) {//Use array as needle<🎜> in in_array() echo "'fi' was foundn";<🎜> }<🎜> if (in_array('o', $a)) {<🎜> echo "'o' was foundn";<🎜> }<🎜> ?> The result of running the program is: (1)Got Irix (2)1.13 found with strict check (3)'ph' was found 'o' was found The above are all one-dimensional arrays. It’s very simple. Let’s see if there is a certain value in the multi-dimensional data
The code is as follows Copy code
$arr = array( array('a', 'b'), array('c', 'd') ); in_array('a', $arr); // The return value at this time is always false deep_in_array('a', $arr); // Returns true value at this time function deep_in_array($value, $array) { foreach($array as $item) { If(!is_array($item)) { If ($item == $value) {                      return true;                 } else { Continue;                }                                                                                                                                                                                           If(in_array($value, $item)) {                  return true;                                                              } else if(deep_in_array($value, $item)) {                 return true;                                                          } }   Return false; } This method is found in the comments under the in_array method detailed explanation page of the PHP help manual. If you have nothing to do, read the help manual more often, especially the classic comments at the end, which collects many people's classic methods.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632800.htmlTechArticleToday we will introduce to you how to determine whether the element value we are looking for exists in the array. Here we will introduce if it is one-dimensional The data is directly in_array but multidimensional data is a little more complicated. Let’s explain it first...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn