Home Backend Development PHP Problem How to define php array

How to define php array

Apr 27, 2023 am 09:10 AM

In PHP, array is a very commonly used data structure. It allows us to store and access data in key-value pairs and is very flexible. This article will introduce in detail the implementation method of PHP array.

  1. Define an array

In PHP, there are two ways to define an array: directly using the array() function or using square brackets []. For example:

$fruits = array('apple', 'orange', 'banana');
$numbers = [1, 2, 3, 4, 5];
Copy after login

When defining an array, you can specify a key name or use the default numeric index. For example:

$person = array('name' => 'John', 'age' => 25);
$colors = ['red', 'green', 'blue'];
Copy after login

You can use the echo and print_r functions to output the elements in the array:

echo $fruits[0]; // 输出apple
print_r($person);
/* 
Array
(
    [name] => John
    [age] => 25
) 
*/
Copy after login
  1. Traverse the array

PHP provides a variety of traversal arrays Methods, the most commonly used ones are foreach loop and for loop.

Use a foreach loop to traverse an array:

foreach($fruits as $fruit) {
    echo $fruit . ',';
}
// 输出apple,orange,banana,

foreach($person as $key => $value) {
    echo $key . ': ' . $value . ', ';
}
// 输出name: John, age: 25,
Copy after login

Use a for loop to traverse an array:

for($i = 0; $i < count($colors); $i++) {
    echo $colors[$i] . &#39;,&#39;;
}
// 输出red,green,blue,
Copy after login
  1. Common operations on arrays

In PHP , there are many common operations on arrays, such as adding, deleting, modifying, merging, etc.

Add an element:

Use square brackets and a new key name to add an element:

$person[&#39;gender&#39;] = &#39;male&#39;;
$colors[] = &#39;yellow&#39;;
Copy after login

Delete an element:

Use the unset function and a key name to Delete elements:

unset($person[&#39;age&#39;]);
Copy after login

Modify elements:

Use the key name and the equal sign to modify the value of the element:

$person[&#39;name&#39;] = &#39;Tom&#39;;
Copy after login

Merge arrays:

Use the array_merge function to Merge arrays:

$more_fruits = [&#39;grape&#39;, &#39;watermelon&#39;];
$fruits = array_merge($fruits, $more_fruits);
Copy after login
  1. Multidimensional arrays

In PHP, you can use arrays to create multidimensional arrays. For example:

$students = array(
    array(&#39;name&#39; => 'Mike', 'age' => 20),
    array('name' => 'Jane', 'age' => 21)
);

echo $students[0]['name']; // 输出Mike
Copy after login

You can also use [] to create a multi-dimensional array:

$students[] = array('name' => 'Bob', 'age' => 22);
Copy after login

Traverse a multi-dimensional array:

foreach($students as $student) {
    echo $student['name'] . ',';
}
// 输出Mike,Jane,Bob,
Copy after login
  1. Array function

PHP provides a wealth of array functions, the following are some of them:

  • count($array): Returns the number of elements in the array
  • array_keys($array): Returns All key names in the array, as elements of the new array
  • array_values($array): Returns all the values ​​in the array, as elements of the new array
  • array_search($value, $array): Find the key name of the specified value in the array
  • array_flip($array): Exchange the key name and value in the array
  1. Summary

PHP array is a very commonly used data structure that can store and access data of key-value pairs. In PHP, you can use square brackets or the array() function to define an array; you can use a foreach loop or a for loop to traverse the array; you can use rich array functions to complete various operations. At the same time, PHP also supports the creation and operation of multi-dimensional arrays. Mastering the implementation method of PHP arrays can allow us to develop and maintain PHP code more efficiently.

The above is the detailed content of How to define php array. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1230
24