What are PHP arrays? How to use PHP arrays?

慕斯
Release: 2023-03-09 22:44:02
Original
2145 people have browsed it

Do you know what a PHP array is? Can you write something about arrays in PHP? This article will take you through PHP arrays, come and learn together! ! !

What are PHP arrays? How to use PHP arrays?

What is an array:

An array is a special variable that can store multiple values ​​in a single variable.

php array, the original meaning is the array in PHP. Its characteristic is that it maps values ​​to the type of keys. Compared with other languages, the keys of arrays in PHP can be strings, and the values ​​can be of any type.

If you have a list of items (for example: a list of car names), store it into a single variable like this:

$cars1="Lamborghini";
$cars2="Maserati";
$cars3="Maybach";
Copy after login

For an array, if you want to iterate over the array And find out a specific one, if the array has not just 1 item but many, then we should create an array at this time! Because array can store multiple values ​​in a single variable and we can access the values ​​in it based on the key.

To create an array in PHP, the syntax is as follows:

array();
Copy after login

In PHP, there are three types of arrays:

  • Numeric array - an array with numeric ID keys

  • # Associative array - an array with specified keys, each key associated with a value

  • Multidimensional array - an array containing one or more arrays

PHP Numeric Array:

here There are two ways to create a numeric array:

Automatic assignment of ID keys (ID keys always start from 0):

$cars=array("Lamborghini","Maserati","Maybach");
Copy after login

Manual assignment ID key :

$cars[0]="Lamborghini";
$cars[1]="Maserati";
$cars[2]="Maybach";
Copy after login

Specifically, we use code to demonstrate:

Copy after login

The running results are as follows:

What are PHP arrays? How to use PHP arrays?

Get the length of the array - count() function:

count() function is used to return the length of the array (number of elements):

Copy after login

The running results are as follows:

What are PHP arrays? How to use PHP arrays?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What are PHP arrays? How to use PHP arrays?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!