In PHP language, array is a widely used data type, and we usually use traversal to operate array elements. However, if we master more efficient array operation methods, we can improve code efficiency and make the program run faster. This article will introduce some methods in PHP that do not need to traverse the array.
The in_array() function is used to determine whether a specified value exists in an array. How to use it is:
in_array(值, 数组, strict);
where value is the value to be found, array is the array in which to find the value, and strict indicates whether to perform type comparison (default is false). Returns true if the value is found, false otherwise.
The following is a sample code:
$my_array = array('apple', 'banana', 'orange'); if (in_array('banana', $my_array)){ echo "找到了"; } else{ echo "没找到"; }
The output result is "found".
The array_key_exists() function is used to determine whether a specified key exists in an array. How to use it is:
array_key_exists(键, 数组);
where key is the key to look for and array is the array in which to look for the key. Returns true if the key is found, false otherwise.
The following is a sample code:
$my_array = array('apple' => 100, 'banana' => 200, 'orange' => 300); if (array_key_exists('banana', $my_array)){ echo "找到了"; } else{ echo "没找到"; }
The output result is "found".
The array_search() function is used to search for a specified value in an array and return its key name. How to use it is:
array_search(值, 数组, strict);
where value is the value to be found, array is the array in which to find the value, and strict indicates whether to perform type comparison (default is false). If the value is found, the corresponding key name is returned, otherwise false is returned.
The following is a sample code:
$my_array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red'); $key = array_search('green', $my_array); echo $key;
The output result is "2".
The array_column() function is used to return a specified column in the array. Its usage is:
array_column(数组, 列名, 键名);
Among them, the array is the array to be taken out of one column, the column name is the name or numerical subscript of the column to be taken out, and the key name is the column to be used as the key name of the new array ( optional parameters). If you want to retrieve multiple columns, you can form an array of column names as the second parameter. If a key is specified, it is used as the key of the new array.
The following is a sample code:
$records = array( array( 'id' => 2135, 'first_name' => 'John', 'last_name' => 'Doe', ), array( 'id' => 3245, 'first_name' => 'Sally', 'last_name' => 'Smith', ), array( 'id' => 5342, 'first_name' => 'Jane', 'last_name' => 'Jones', ), array( 'id' => 5623, 'first_name' => 'Peter', 'last_name' => 'Doe', ) ); $last_names = array_column($records, 'last_name', 'id'); print_r($last_names);
The output result is:
Array ( [2135] => Doe [3245] => Smith [5342] => Jones [5623] => Doe )
array_merge() Function is used to combine one or more arrays into a single array. Its usage is:
array_merge(数组1, 数组2, ..., 数组n);
Among them, array 1 to array n are the arrays to be merged, and their elements will be merged into one array. If the same key name exists in the array, the later value will overwrite the previous value.
The following is a sample code:
$a = array('a', 'b', 'c'); $b = array('d', 'e', 'f'); $c = array_merge($a, $b); print_r($c);
The output result is:
Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f )
Summary:
The above are the methods in PHP that do not need to traverse the array. These Functions allow us to operate arrays more conveniently and efficiently. Of course, different functions may be needed in different scenarios, and they must be used flexibly to better improve code efficiency.
The above is the detailed content of PHP does not need to traverse the operation array. For more information, please follow other related articles on the PHP Chinese website!