Home  >  Article  >  Backend Development  >  Where is the php array created?

Where is the php array created?

PHPz
PHPzOriginal
2023-04-25 09:03:01375browse

PHP is a widely used server-side programming language. PHP arrays are a very useful data structure for efficiently storing and manipulating multiple data elements in a program. In PHP, creating an array does not need to be an independent data type like other programming languages, but can be directly defined as a variable type. This article explains how PHP arrays are created and the details.

  1. Create index array

Index array is the most commonly used type of PHP array. Its keys are numeric, starting from 0 and increasing, and each element is associated with a numeric index. To create an indexed array, you can use the following syntax:

$array = array('foo', 'bar', 'baz');

or:

$array = ['foo', 'bar', 'baz'];

This will create an array with three elements, with keys 0, 1, and 2.

Of course, this is just the most basic usage. You can also use loop constructs similar to those in C or other programming languages ​​to dynamically create indexed arrays containing any number of elements, for example:

$score = array();
for ($i = 0; $i < 5; $i++) {
    $score[$i] = rand(60, 100);
}

This code will create an array containing 5 random integers.

  1. Create an associative array

Associative array is another common type of PHP array, and it is also an array type with strings or numbers as keys. Associative arrays can contain various data types, such as strings, integers, floating point numbers, and Boolean values. Associative arrays can be defined using array() or [].

$person = array(
    &#39;name&#39; => 'Tom',
    'age' => 18,
    'gender' => 'Male'
);

Or:

$person = [
    'name' => 'Tom',
    'age' => 18,
    'gender' => 'Male'
];

This defines an associative array containing three elements, each element has its own key value. It should be noted that the key values ​​of an associative array can be of any type, but the values ​​must be within the range allowed by the PHP language.

  1. Creating multi-dimensional arrays

In PHP, you can also create multi-dimensional arrays, which are a data structure formed by arrays within arrays. Common multidimensional arrays include two-dimensional arrays and three-dimensional arrays. Multidimensional arrays can be defined in a nested manner similar to two-dimensional arrays.

$people = array(
    array('name' => 'Tom', 'age' => 18, 'gender' => 'Male'),
    array('name' => 'Alice', 'age' => 20, 'gender' => 'Female')
);

Or:

$people = [
    ['name' => 'Tom', 'age' => 18, 'gender' => 'Male'],
    ['name' => 'Alice', 'age' => 20, 'gender' => 'Female']
];

A two-dimensional array is defined here, which contains two elements, each element is an associative array containing three key values.

If you need to create a three-dimensional array, you can use a method similar to the following:

$students = array(
    array(
        array('name' => 'Tom', 'age' => 18),
        array('name' => 'Jack', 'age' => 19),
    ),
    array(
        array('name' => 'Alice', 'age' => 20),
        array('name' => 'Linda', 'age' => 22),
    )
);

Or:

$students = [
    [
        ['name' => 'Tom', 'age' => 18],
        ['name' => 'Jack', 'age' => 19],
    ],
    [
        ['name' => 'Alice', 'age' => 20],
        ['name' => 'Linda', 'age' => 22],
    ]
];

A three-dimensional array is defined here, which contains two elements, each element Contains two one-dimensional arrays with two key values.

Summary

The creation of PHP arrays is easy, whether it is an index array, an associative array, or a multi-dimensional array, it can be easily implemented. When creating an array, you only need to use array() or [] to define it and add the corresponding elements to it. In addition, you can also use related functions or loops and other structures to dynamically create arrays to meet actual needs. Mastering the skills of creating PHP arrays can bring great convenience to programmers in actual work.

The above is the detailed content of Where is the php array created?. 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