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

How to define php array

PHPz
Release: 2023-04-27 10:08:51
Original
560 people have browsed it

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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template