Home  >  Article  >  Backend Development  >  Deep talk about what is an array in php?

Deep talk about what is an array in php?

PHP中文网
PHP中文网Original
2017-10-27 09:04:471094browse

What is an array?

The registry is a huge database. The key value is located at the end of the registry structure chain, similar to the files in the file system, and contains the actual configuration information and data used when the current computer and application are executed.

The array is a language structure composed of key-value pairs. The key is similar to the hotel room number, and the value is similar to the things stored in the hotel room.

Create an empty array: $arr=array();

PHP array array can store multiple values ​​in a single variable:

<?php
$cars=array(
    "Volvo",
    "BMW",
    "Toyota"
    );
    echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

What is an array?

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

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

<?php
    $cars1="Volvo";
    $cars2="BMW";
    $cars3="Toyota";
?>

However, if you want to iterate through the array and find What about a specific one? What if the array has not just 3 items but 300?

The solution is to create an array!

Arrays can store multiple values ​​in a single variable, and you can access the values ​​within them based on their keys.

Creating arrays in PHP

In PHP, the array() function is used to create arrays:

array();

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 is associated with a value

Multidimensional array - contains one or more Array of Arrays

PHP Numeric Array

There are two ways to create a numeric array:

Automatically assign ID keys (ID keys always start from 0):

$cars=array("Volvo","BMW","Toyota");
//人工分配 ID 键:
$cars[0]="Volvo";$cars[1]="BMW";$cars[2]="Toyota";

The following example creates a numerical array named $cars, assigns three elements to the array, and then prints a text containing the array value:

Example

<?php
    $cars=array("Volvo","BMW","Toyota");echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

Get The length of the array - count() function

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

Example

<?php
    $cars=array("Volvo","BMW","Toyota");echo count($cars);
?>

Traverse the numerical array

Traverse and print all the values ​​in the numeric array, you can use a for loop, as shown below:

Example

<?php 
    $cars=array("Volvo","BMW","Toyota");$arrlength=count($cars);
     for($x=0;$x<$arrlength;$x++){
   
             echo $cars[$x];    echo "<br>";
    }
?>

PHP associative array

Associative array is used The array that you assign to the specified key of the array.

There are two ways to create an associative array:

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
or:
$age[&#39;Peter&#39;]="35";$age[&#39;Ben&#39;]="37";$age[&#39;Joe&#39;]="43";

The specified key can then be used in a script:

Example

<?php 
    $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");echo "Peter is " . $age[&#39;Peter&#39;] . " years old.";
 ?>

Traverse the associative array

To traverse and print all values ​​in an associative array, you can use a foreach loop, as shown below:

Example

<?php 
    $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); 
        foreach($age as $x=>$x_value){
        echo "Key=" . $x . ", Value=" . $x_value;    echo "<br>";
    }
 ?>


foreach Syntax constructs provide a simple way to traverse arrays. foreach can only be applied to arrays and objects. If you try to apply it to variables of other data types, or uninitialized variables, an error message will be issued. There are two syntaxes:

foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement

The first format iterates over the given array_expression array. Each time through the loop, the value of the current cell is assigned to $value and the pointer inside the array is moved forward one step (so the next cell will be obtained in the next loop).

The second format does the same thing, except that the key name of the current unit will also be assigned to the variable $key in each loop.


The above is the detailed content of Deep talk about what is an array 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