Home >Backend Development >PHP Problem >How to use arrays in php

How to use arrays in php

PHPz
PHPzOriginal
2023-04-17 14:14:03573browse

Array is a very important data type in PHP, which allows you to store and process large amounts of data. PHP provides many built-in functions to operate on arrays, including adding, searching, sorting, and deleting arrays.

This article will introduce you to the basic knowledge and common operations of PHP arrays.

1. Definition of array

In PHP, arrays can be defined in the following ways:

Use the array() function to define:

$fruit = array("apple", "banana", "orange");

Use the [] symbol to define:

$fruit = ["apple", "banana", "orange" "];

Use the range() function definition:

$numbers = range(1, 10);

The above three methods can all create and initialize an array. .

2. Array access

The elements in the array can be accessed through the subscript (index) of the array, and the subscript starts from 0. For example, to access the first element in the $fruit array defined above, you can use the following method:

echo $fruit[0];

The output result is: apple

If you want to traverse the entire array, you can use a for loop:

for ($i=0; $i

echo $fruit[$i];

}

This code will output all elements in the array.

3. Adding and deleting arrays

To add elements to an array, you can use the following built-in functions:

array_push(): push one or more elements Added to the end of the array.

array_unshift(): Adds one or more elements to the beginning of the array.

array_merge(): Merge one or more arrays into a new array.

For example:

$fruit = ["apple", "banana"];
array_push($fruit, "orange");
array_unshift($fruit, "grape ");
$new_fruit = array_merge($fruit, ["pineapple", "watermelon"]);

The above code will add orange elements to the $fruit array and add grape elements at the beginning. In addition, the $fruit array and a new array containing two elements are merged into a new array $new_fruit through array_merge().

To remove elements from an array, you can use the following two built-in functions:

array_pop(): Remove an element from the end of the array.

array_shift(): Remove an element from the beginning of the array.

For example:

$fruit = ["apple", "banana", "orange"];
$last_fruit = array_pop($fruit);
$first_fruit = array_shift ($fruit);

The above code will remove the last element from the $fruit array and store it in the $last_fruit variable. Then, remove the first element from $fruit and store it in the $first_fruit variable.

4. Search and sorting of arrays

To search for elements in an array, you can use the following built-in functions:

in_array(): Find the specified value in the array, Returns true if found, false otherwise.

array_search(): Search for the specified value in the array, return the index if found, otherwise return false.

For example:

$fruit = ["apple", "banana", "orange"];
if (in_array("apple", $fruit)) {

echo "apple is found in the array";

} else {

echo "apple is not found in the array";

}

The above code will search for apple in the $fruit array. If found, "apple is found in the array" will be output, otherwise "apple is not found in the array" will be output.

To sort an array, you can use the following built-in functions:

sort(): Sort the array in ascending order.

rsort(): Sort the array in descending order.

For example:

$numbers = [3, 5, 1, 4, 2];
sort($numbers);
print_r($numbers);

The above code will sort the $numbers array in ascending order and output the sorted results.

5. Summary

PHP arrays are an important tool when processing large amounts of data. In this article, we introduce the basic knowledge and common operations of PHP arrays, including array definition, access, addition, deletion, search and sorting. By mastering these skills, you can work with arrays in PHP more efficiently.

The above is the detailed content of How to use arrays 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