PHP Determines whether a multidimensional array has a value

Source: Internet
Author: User
Tags comments manual

Let's take a look at the In_array. Check if a value exists in the array

The code is as follows

<?php
$os = Array ("Mac", "NT", "Irix", "Linux");

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 (' 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 () is used as an array needle
echo "' fi ' was Foundn";
}
if (In_array (' o ', $a)) {
echo "' O ' was Foundn";
}
?>

The results of the program run are:

(1) Got Irix

(2) 1.13 found with strict check

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

They're all one-dimensional arrays. It's easy to see if the multidimensional data has a value


The code is as follows

$arr = Array (
Array (' A ', ' B '),
Array (' C ', ' d ')
);

In_array (' A ', $arr); The return is always false at this time
Deep_in_array (' A ', $arr); Returns a value of true 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 in the PHP Help manual In_array method detailed page comments see, usually nothing more to see the Help manual, especially behind the classic comments, which collected a lot of people's classic methods Ah.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.