Home  >  Article  >  Backend Development  >  Basic operations and usage of arrays in PHP

Basic operations and usage of arrays in PHP

WBOY
WBOYOriginal
2023-06-28 20:02:531306browse

Basic operations and usage of arrays in PHP

1. Overview
Array is a very important data type in PHP. It can be used to store multiple values, and can Access these values ​​via index or key. Arrays have rich operations and usage methods in PHP. This article will introduce in detail the basic operations and usage methods of arrays in PHP.

2. Create arrays
In PHP, you can create arrays in two ways: countable arrays and associative arrays.

  1. Create a countable array
    A countable array is an array that is arranged in order and indexed numerically. The following is how to create a countable array:

// Method 1: Use the array() function
$numbers = array(1, 2, 3, 4, 5);

// Method 2: Use [] (available from PHP 5.4)
$numbers = [1, 2, 3, 4, 5];

  1. Create an associative array
    Associative array is an array stored with string keys and corresponding values. The following is how to create an associative array:

// Method 1: Use the array() function
$student = array("name" => "John", "age" => ; 18, "grade" => "A");

// Method 2: Use [] (available starting from PHP 5.4)
$student = ["name" => "John ", "age" => 18, "grade" => "A"];

3. Accessing array elements
You can access elements in the array through indexes or keys.

  1. Accessing countable arrays
    For countable arrays, you can use indexing to access array elements. The index starts from 0 and increases sequentially.

$numbers = array(1, 2, 3, 4, 5);

echo $numbers[0]; // Output 1
echo $numbers[2 ]; // Output 3

  1. Accessing associative arrays
    For associative arrays, you can use keys to access array elements.

$student = array("name" => "John", "age" => 18, "grade" => "A");

echo $student["name"]; // Output John
echo $student["age"]; // Output 18

4. Common array operations and functions

  1. Get the length of the array
    Use the count() function to get the number of elements in the array.

$numbers = array(1, 2, 3, 4, 5);

echo count($numbers); // Output 5

  1. Traverse the array
    PHP provides a variety of methods to traverse the array, the most common method is to use the foreach loop.

$numbers = array(1, 2, 3, 4, 5);

foreach ($numbers as $number) {

echo $number . " ";

}
//Output 1 2 3 4 5

  1. Add elements
    You can use the [] or array_push() function to add elements to the array.

$numbers = array(1, 2, 3, 4, 5);

// Use [] to add elements
$numbers[] = 6;

// Use array_push() to add elements
array_push($numbers, 7);

print_r($numbers);
// Output Array (1, 2, 3, 4 , 5, 6, 7)

  1. Delete elements
    You can use the unset() function to delete elements in the array.

$numbers = array(1, 2, 3, 4, 5);

unset($numbers[2]);

print_r($numbers );
// Output Array (1, 2, 4, 5)

5. Summary
This article introduces the basic operations and usage of arrays in PHP, including creating arrays and accessing array elements , get the array length, traverse the array, and add and remove elements. Arrays are a very important data type in PHP and are often used in actual development. Mastering the basic operations and usage of arrays can help developers process data more flexibly and efficiently.

The above is the detailed content of Basic operations and usage 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