PHP does not need to traverse the operation array

WBOY
Release: 2023-05-19 18:08:08
Original
567 people have browsed it

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.

  1. Use the in_array() function

The in_array() function is used to determine whether a specified value exists in an array. How to use it is:

in_array(值, 数组, strict);
Copy after login

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 "没找到";
}
Copy after login

The output result is "found".

  1. Use the array_key_exists() function

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(键, 数组);
Copy after login

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 "没找到";
}
Copy after login

The output result is "found".

  1. Using the array_search() function

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);
Copy after login

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;
Copy after login

The output result is "2".

  1. Using the array_column() function

The array_column() function is used to return a specified column in the array. Its usage is:

array_column(数组, 列名, 键名);
Copy after login

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);
Copy after login

The output result is:

Array
(
    [2135] => Doe
    [3245] => Smith
    [5342] => Jones
    [5623] => Doe
)
Copy after login
  1. Use the array_merge() function

array_merge() Function is used to combine one or more arrays into a single array. Its usage is:

array_merge(数组1, 数组2, ..., 数组n);
Copy after login

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);
Copy after login

The output result is:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
)
Copy after login

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!

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