What is the purpose of using array_search in multidimensional arrays in PHP?

When working with multidimensional arrays in PHP, it can be challenging to search for a specific value within the nested arrays. The array_search function in PHP allows you to search for a specific value within a multidimensional array and returns the corresponding key if the value is found. This can be useful for tasks such as finding a specific item in a list of products or searching for a particular user in a list of user data.

// Example of using array_search in a multidimensional array
$users = array(
    array('id' => 1, 'name' => 'Alice'),
    array('id' => 2, 'name' => 'Bob'),
    array('id' => 3, 'name' => 'Charlie')
);

$searchName = 'Bob';

foreach ($users as $key => $user) {
    if (in_array($searchName, $user)) {
        $foundKey = $key;
        break;
    }
}

if (isset($foundKey)) {
    echo "User found at index: " . $foundKey;
} else {
    echo "User not found";
}