PHP Multidimensional Array Search Guide

To search for a specific value in a multi-dimensional array, you can use a loop to iterate through the array to find it. Here is a simple example code:

// 定义一个多维数组
$multiArray = array(
    array(
        'name' => 'Alice',
        'age' => 25
    ),
    array(
        'name' => 'Bob',
        'age' => 30
    ),
    array(
        'name' => 'Charlie',
        'age' => 35
    )
);

// 遍历多维数组查找特定值
$searchValue = 'Bob';
foreach ($multiArray as $subArray) {
    if (in_array($searchValue, $subArray)) {
        echo "找到了!";
        break;
    }
}

// 如果查找不到该值
echo "未找到该值!";

In the example above, we defined a multi-dimensional array $multiArray and then used a foreach loop to iterate through the array. We then used the in_array function to search for the existence of the specific value ‘Bob’. If the value is found, it will output ‘Found!’, otherwise it will output ‘Value not found!’.

bannerAds