Home  >  Article  >  Backend Development  >  The basic way to write arrays in php

The basic way to write arrays in php

WBOY
WBOYOriginal
2023-05-07 20:13:07524browse

PHP is a popular web development language that provides a very powerful array type for organizing and storing data. In PHP, an array is a complex data structure that holds multiple values, which can be variables of any type. In this article, we will discuss in depth the basics of writing arrays in PHP.

Define an array

In PHP, you can use different methods to define an array. The following are three common ways:

$a = array('apple', 'banana', 'cherry');

$b = ['dog', 'cat' , 'bird'];

$c[0] = 'red';

$c[1] = 'blue';

$c[2] = 'green';

In the above code snippet, we define three arrays: $a, $b and $c. Among them, $a and $b are defined by using array literals. This is the most common way and the easiest to understand. Note that PHP array indexing starts from 0.

Access to array values

You can use indexing to access elements in an array. An index is a unique identifier that identifies each element in an array. Here are some examples:

echo $a[0]; //Output: apple

echo $b[1]; //Output: cat

echo $c [2]; //Output: green

Use a loop to traverse an array

You can use a loop variable to traverse the elements in an array. Here are some examples:

for ($i=0; $i

echo $a[$i] . ' ';

}

//Output: apple banana cherry

foreach($b as $value) {

echo $value . ' ';

}

//Output: dog cat bird

while (list($key, $value) = each($c)) {

echo $value . ' ';

}

//Output: red blue green

In the above example, we used for, foreach and while loops to traverse $a, $ elements in the b and $c arrays. Among them, the foreach loop is the most versatile and commonly used traversal method, and it is easier to use than other loops.

Adding and Removing Elements

You can add an element to the end of an array by using the array_push() function. The following is an example:

array_push($a, 'orange'); //Add an element to the $a array

echo count($a); //Output: 4

In the above example, we added a new element 'orange' to the $a array and used the count() function to check whether the length of the array has increased.

You can use the unset() function to delete an element from an array. Here is an example:

unset($b[1]); //Remove the second element from the $b array

echo count($b); //Output: 2

In the above example, we used the unset() function to delete the second element in the $b array and used the count() function to check whether the length of the array was reduced.

Conclusion

PHP’s arrays provide a very flexible and convenient way to organize and store data. In this article, we have discussed the basics of writing arrays, including defining arrays, accessing elements, looping over arrays, and adding and removing elements. Hope this article is helpful to beginners.

The above is the detailed content of The basic way to write 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