Home  >  Article  >  Backend Development  >  PHP determines whether it is in a certain attribute of the array

PHP determines whether it is in a certain attribute of the array

WBOY
WBOYOriginal
2023-05-05 21:53:09580browse

In PHP, operations on arrays are very frequent. An array is an unordered data structure that can store multiple associated values. When we need to determine whether an element exists in an array, we can use PHP's in_array function to determine. But if you need to determine whether an element exists in a certain attribute of the array, how to implement it?

  1. Use a for loop to traverse the array

We can use a for loop to traverse the array and compare whether a certain attribute of each element meets the conditions one by one. If an attribute that meets the conditions is found element, it means that the element exists in this attribute of the array.

Sample code:

function isExistInArray($needle, $array, $key) {
    for ($i = 0; $i < count($array); $i++) {
        if ($array[$i][$key] == $needle) {
            return true;
        }
    }
    return false;
}

$array = array(
    array("name" => "apple", "color" => "red"),
    array("name" => "banana", "color" => "yellow"),
    array("name" => "orange", "color" => "orange")
);

echo isExistInArray("red", $array, "color") ? "存在" : "不存在"; // 存在
echo isExistInArray("green", $array, "color") ? "存在" : "不存在"; // 不存在

In the above code, we define a function named isExistInArray. This function has three parameters: the element to be found $needle, the array to be found $array, and the attribute name $key. Compare the attribute values ​​of each element one by one through a for loop. If an element that meets the conditions is found, true is returned, otherwise false is returned.

  1. Using the array_map function

The array_map function can be used to execute a specified function on each element of the array and return the processed array. We can use this function to filter a certain attribute of the array to determine whether an element exists in the attribute.

Sample code:

function checkValue($value, $needle) {
    if ($value == $needle) {
        return true;
    }
    return false;
}

$array = array(
    array("name" => "apple", "color" => "red"),
    array("name" => "banana", "color" => "yellow"),
    array("name" => "orange", "color" => "orange")
);

$result = array_map(function($item) use ($needle) {
    return checkValue($item["color"], $needle);
}, $array);

if (in_array(true, $result)) {
    echo "存在";
} else {
    echo "不存在";
}

In the above code, we define a function named checkValue to compare whether a certain attribute value of the array is equal to the value to be found. Next, we use the array_map function to execute the checkValue function on each element of the array and return the processed array. Finally, we use the in_array function to determine whether there is a true value in the processed array. If it exists, it means that the element exists in this attribute of the array.

In short, to determine whether an element exists in a certain attribute of the array, you need to traverse the array or use the array_map function to filter the array. The above two methods each have their own advantages and disadvantages, and developers can choose the appropriate method according to their actual needs.

The above is the detailed content of PHP determines whether it is in a certain attribute of the array. For more information, please follow other related articles on the PHP Chinese website!

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