How to find whether an element exists in an array in php

WBOY
Release: 2016-07-25 08:52:10
Original
1426 people have browsed it
How to find whether an element exists in a php array? You can consider using array_key_exists, in_array, array_search and other functions. This article will give you a brief introduction and interested friends can use it as a reference.

Contents of this section: Find an element in an array

In PHP programming, there are three ways to find whether an element is in an array: in_array 'Function searches an array for a given value. in_array(value,array,type)type optional. If this parameter is set to true, it is checked whether the type of the searched data and the value of the array are the same.

array_key_exists 'array_key_exists() function Determine whether the specified key exists in an array. If the key exists, return true, otherwise return false.

array_search 'array_search() function Like in_array(), finds a key value in an array. If the value is found, the key of the matching element is returned. If not found, returns false.

Instructions: When the amount of data is not large, such as less than 1000, any search method can be used and it will not become a bottleneck; When the amount of data is relatively large, array_key_exists is more appropriate. Of course, the memory occupied by array_key_exists here is relatively large. After calculation, the array structure is:

array(1, 2, 3, ..) and array(1 => true, 2 => false, ..)

Memory usage ratio is 1:2; This has something to do with the internal implementation. In fact, the first and second data structures in PHP are similar, both are associative arrays.



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!