Home >Backend Development >PHP Problem >How to write php array

How to write php array

PHPz
PHPzOriginal
2023-04-19 11:35:48419browse

PHP is a popular web programming language that provides a convenient way to process data. Arrays in PHP are one of the very powerful features that help you organize and process various data types.

In this article, we will discuss how to use PHP arrays. We will cover the following topics:

  1. What is an Array
  2. Declaring an Array
  3. Accessing Array Elements
  4. Array Operations
  5. Multidimensional Array
  6. What is an array

An array is a collection of related variable values, each value is assigned a unique key. In PHP, arrays can store any type of data, including numbers, strings, and objects. Arrays can make data storage, organization, and access more convenient and efficient.

  1. Declaring arrays

In PHP, there are two types of arrays: indexed arrays and associative arrays. Indexed arrays use numeric keys to match variable values, while associative arrays use string keys to match variable values.

Declare an indexed array:

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

You can also use a simpler syntax to declare this array:

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

Declare an associative array:

$my_array = array("name" => "John", "age" => 30, "city" => "New York");

You can also use the simpler syntax to declare this array:

$my_array = ["name" => "John", "age" => 30, "city" => "New York"];
  1. Accessing Array Elements

You can use the following syntax to get elements from the array:

$array[index];

In an index array, index is a number that represents the position of the element you want to access. In an associative array, index is a string that represents the key of the element you want to access.

Here's how to access elements in an array:

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

echo $fruits[0]; // 输出 "apple"

$person = array("name" => "John", "age" => 30, "city" => "New York");

echo $person["name"]; // 输出 "John"
  1. Array operations

PHP provides many convenient functions to operate on arrays. Here are some common array operations:

  • Add new elements

One of the ways to add new elements to an array is to use the following syntax:

$array[] = $value;

The following example adds a new element to an array:

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

$fruits[] = "grape";

print_r($fruits); // 输出 Array ( [0] => apple [1] => banana [2] => orange [3] => grape )
  • Deleting an element

You can use the following function to delete an element from an array:

unset()

The following example removes the first element from an indexed array:

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

unset($fruits[0]);

print_r($fruits); // 输出 Array ( [1] => banana [2] => orange )

array_splice()

The following example uses the array_splice function to remove the first element from an indexed array:

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

array_splice($fruits, 0, 1);

print_r($fruits); // 输出 Array ( [0] => banana [1] => orange )
  • Array sorting

You can use the following function to sort the array:

sort()

The following example uses the sort function to sort the index Sort an array in ascending order:

$numbers = array(5, 3, 8, 2, 9);

sort($numbers);

print_r($numbers); // 输出 Array ( [0] => 2 [1] => 3 [2] => 5 [3] => 8 [4] => 9 )

asort()

The following example uses the asort function to sort an associative array in ascending order by value:

$person = array("name" => "John", "age" => 30, "city" => "New York");

asort($person);

print_r($person); // 输出 Array ( [age] => 30 [name] => John [city] => New York )
  1. Multidimensional array

PHP also supports multi-dimensional arrays, which means you can have one array as an element of another array. The following is an example:

$students = array(
    array("name" => "John", "age" => 20),
    array("name" => "Mary", "age" => 22),
    array("name" => "Tom", "age" => 21)
);

echo $students[1]["name"]; // 输出 "Mary"

The above is a brief introduction to PHP arrays. I hope you can learn some useful knowledge and improve your programming skills.

The above is the detailed content of How to write php array. 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