Home > Backend Development > PHP Problem > What are the commonly used array functions and methods in PHP?

What are the commonly used array functions and methods in PHP?

尊渡假赌尊渡假赌尊渡假赌
Release: 2023-06-07 16:01:00
Original
1332 people have browsed it

Commonly used array functions and methods in PHP are: 1. The "array_push()" function adds one or more elements to the end of the array; 2. The "count()" function returns the number of elements in the array; 3. The "array_pop()" function deletes and returns the elements at the end of the array; 4. The "array_shift()" function deletes and returns the elements at the beginning of the array; 5. The "sort()" function sorts the array in ascending order.

What are the commonly used array functions and methods in PHP?

Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.

Commonly used array functions and methods in PHP are:

1. array_push() adds one or more elements to the end of the array

Example :

$my_array = array("apple", "banana");
array_push($my_array, "orange", "peach");
print_r($my_array);
Copy after login

The result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => peach
)
Copy after login

2. count() returns the number of elements in the array

Example:

$my_array = array("apple", "banana", "orange");
$count = count($my_array);
echo "The count of my_array is " . $count;
Copy after login

The result is:

The count of my_array is 3
Copy after login

3. array_pop() deletes and returns the element at the end of the array

Example:

$my_array = array("apple", "banana", "orange");
$last_element = array_pop($my_array);
print_r($my_array);
echo "The last element was " . $last_element;
Copy after login

Result For:

Array
(
    [0] => apple
    [1] => banana
)
The last element was orange
Copy after login

4, array_shift() deletes and returns the element at the beginning of the array

Example:

$my_array = array("apple", "banana", "orange");
$first_element = array_shift($my_array);
print_r($my_array);
echo "The first element was " . $first_element;
Copy after login

The result is:

Array
(
    [0] => banana
    [1] => orange
)
The first element was apple
Copy after login

5. sort() sorts the array in ascending order

Example:

$my_array = array(9, 2, 5, 3, 8);
sort($my_array);
print_r($my_array);
Copy after login

The result is:

Array
(
    [0] => 2
    [1] => 3
    [2] => 5
    [3] => 8
   )
Copy after login

The above is the detailed content of What are the commonly used array functions and methods in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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