Home  >  Article  >  Backend Development  >  Let’s talk about several uses of arrays in php

Let’s talk about several uses of arrays in php

PHPz
PHPzOriginal
2023-04-18 10:26:14597browse

PHP is a popular server-side scripting language. Its powerful array function facilitates developers' ability to process data when developing network applications. This article will introduce several uses of arrays in PHP.

  1. Create Array

In PHP, arrays can be created in two ways: one is to use the array() function, the other is to use square brackets [ ] to define.

The syntax for using array() to define an array is as follows:

$array = array(element1,element2, element3......);

The syntax for using square brackets [] to define an array is as follows:

$array = [element1,element2, element3......];

Among them, element1, element2, element3... represent Array elements.

For example, we can create an array containing three elements:

$color = array("red", "green", "blue");
//或者
$color = ["red", "green", "blue"];
  1. Accessing array elements

The way to access array elements is to use the array mark. Array subscripts in PHP can be of numeric or string type.

Subscripts start counting from 0. In the array definition, separate each element with a comma. We can use subscripts to access elements in the array, for example:

$color = array("red", "green", "blue");
echo $color[0]; // 输出 red
echo $color[1]; // 输出 green
echo $color[2]; // 输出 blue

We can also use the foreach loop structure to traverse each element in the array, as shown below:

$color = array("red", "green", "blue");

foreach ($color as $value) {
    echo $value . "<br>";
}

This paragraph The code will output each element in the array in turn, with a newline character after each element.
red
green
blue

In the foreach loop structure, the $value variable represents each element in the array, which can be operated on within the loop body.

If you want to access the key name and key value in the array at the same time, you can use another foreach loop structure, as shown below:

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $key => $value) {
    echo "Key=" . $key . ", Value=" . $value."<br>";
}

This code will output the key of each array element name and key value.
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43

In this loop structure, the $key variable represents each The key name of each element, and the $value variable represents the key value of each element. The operations within the loop body can operate on the key names and key values ​​of the array respectively.

  1. Built-in functions for arrays

PHP provides many built-in functions to handle arrays. The following are some commonly used array functions:

(1) count() function

The count() function is used to return the number of elements in the array, as shown below:

$color = array("red", "green", "blue");
echo count($color); // 输出 3

(2) array_push() and array_pop() functions

array_push() is used to add one or more elements to the end of the array, and array_pop() is used to delete the last element in the array.

$color = array("red", "green");
array_push($color, "blue", "yellow");
print_r($color); // 输出 array("red", "green", "blue", "yellow")

$color = array("red", "green", "blue");
array_pop($color);
print_r($color); // 输出 array("red", "green")

(3) array_shift() and array_unshift() functions

array_shift() deletes the first element in the array and returns the element, array_unshift() adds an or at the beginning of the array Multiple elements.

$color = array("red", "green", "blue");
echo array_shift($color); // 输出 red
print_r($color); // 输出 array("green", "blue")

$color = array("red", "green", "blue");
array_unshift($color, "yellow", "orange");
print_r($color); // 输出 array("yellow", "orange", "red", "green", "blue")

(4) in_array() function

in_array() is used to search for the specified element in the array. If found, it returns true, otherwise it returns false.

$color = array("red", "green", "blue");
echo in_array("red", $color); // 输出 1
echo in_array("black", $color); // 输出 空

1 here means true, empty means false.

(5) sort() and rsort() functions

sort() is used to sort the array in ascending order, and rsort() is used to sort the array in descending order.

$color=array("blue","red","green");
sort($color);
print_r($color); //输出 array("blue", "green", "red")

$color=array("blue","red","green");
rsort($color);
print_r($color); //输出 array("red", "green", "blue")

The above functions are just the tip of the iceberg of array functions. They are sufficient to handle most cases, but in reality, PHP provides many more functions for working with arrays.

Summary

This article introduces the creation and access of arrays in PHP and the usage of some built-in array functions. Understanding the usage of arrays is one of the skills that web developers must master. Use built-in functions to let you write programs faster and manage your data better. Hope this information is helpful to you.

The above is the detailed content of Let’s talk about several uses of 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