Home  >  Article  >  Backend Development  >  Let’s talk about commonly used array methods in php

Let’s talk about commonly used array methods in php

PHPz
PHPzOriginal
2023-04-23 10:21:03380browse

PHP is a widely used server-side scripting language used for web development and building dynamic websites. In PHP, array is one of the most commonly used data structures and its use is very widespread. This article will introduce commonly used array methods in PHP to help PHP developers better use and understand arrays.

1. Definition and declaration of array

In PHP, an array is a data structure used to store a set of values. It can store different types of values ​​at the same time, such as strings, integers, Floating point numbers, objects, etc.

Declare an array in the following ways:

  1. Use the array() function:
$array = array('a', 'b', 'c');
  1. Use square brackets:
$array = ['a', 'b', 'c'];
  1. Use the array() function and key-value pairs:
$array = array('name' => 'John', 'age' => 25);
  1. Use square brackets and key-value pairs:
$array = ['name' => 'John', 'age' => 25];

2. Commonly used array methods

  1. count function

The count function is used to return the length of the array, that is, the number of elements in the array. For example:

$array = ['a', 'b', 'c'];
$count = count($array); // $count的值为3
  1. in_array function

The in_array function is used to determine whether a value exists in an array. For example:

$array = ['a', 'b', 'c'];
if (in_array('b', $array)) {
    echo '数组中存在b';
} else {
    echo '数组中不存在b';
}

The result output is "b exists in the array".

  1. array_search function

The array_search function is used to search for a value in an array and return the key name corresponding to the value. For example:

$array = ['name' => 'John', 'age' => 25];
$key = array_search('John', $array); // $key的值为'name'
  1. array_key_exists function

array_key_exists function is used to determine whether a key name exists in the array. For example:

$array = ['name' => 'John', 'age' => 25];
if (array_key_exists('name', $array)) {
    echo '数组中存在name';
} else {
    echo '数组中不存在name';
}

The result output is "name exists in the array".

  1. array_push function and array_pop function

The array_push function is used to insert one or more elements at the end of the array, and the array_pop function is used to delete elements at the end of the array. For example:

$array = ['a', 'b', 'c'];
array_push($array, 'd'); // 数组变为 ['a', 'b', 'c', 'd']
$last = array_pop($array); // $last的值为'd',数组变为 ['a', 'b', 'c']
  1. array_shift function and array_unshift function

The array_shift function is used to delete elements at the beginning of the array, and the array_unshift function is used to insert one or more elements at the beginning of the array. For example:

$array = ['a', 'b', 'c'];
$first = array_shift($array); // $first的值为'a',数组变为 ['b', 'c']
array_unshift($array, 'x', 'y'); // 数组变为 ['x', 'y', 'b', 'c']
  1. array_slice function

array_slice function is used to remove a continuous element from an array and return a new array. For example:

$array = ['a', 'b', 'c', 'd', 'e'];
$slice = array_slice($array, 1, 3); // $slice的值为 ['b', 'c', 'd']
  1. array_merge function

array_merge function is used to merge two or more arrays into one array. For example:

$array1 = ['a', 'b', 'c'];
$array2 = ['d', 'e', 'f'];
$merged = array_merge($array1, $array2); // $merged的值为 ['a', 'b', 'c', 'd', 'e', 'f']
  1. array_reverse function

array_reverse function is used to flip the array. For example:

$array = ['a', 'b', 'c'];
$reversed = array_reverse($array); // $reversed的值为 ['c', 'b', 'a']
  1. array_fill function

array_fill function is used to create an array consisting of a specified number of elements and initialize each element to the same value. For example:

$filled = array_fill(0, 5, 'a'); // $filled的值为 ['a', 'a', 'a', 'a', 'a']

3. Summary

Arrays are one of the most commonly used data structures in PHP. During the development process, mastering common array operation methods is very helpful for improving development efficiency and code quality. help. This article introduces commonly used array methods in PHP, including count, in_array, array_search, array_key_exists, array_push, array_pop, array_shift, array_unshift, array_slice, array_merge, array_reverse and array_fill methods. These methods are simple to use, powerful, and are often used in various scenarios in PHP development.

The above is the detailed content of Let’s talk about commonly used array methods in php. 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