Home  >  Article  >  Backend Development  >  There are several array initialization methods in php. What are they?

There are several array initialization methods in php. What are they?

WBOY
WBOYOriginal
2023-05-22 19:38:051071browse

PHP is a scripting language widely used in Web development. Its arrays have multiple initialization methods. This article will introduce the different initialization methods of PHP arrays and explain them in detail.

1. Define an empty array

An empty array is the simplest way to initialize it. It can be done in the following ways:

$array = array();

or

$array = [];

If you put the above code snippet into a PHP script and execute it, you will find that the array size is 0 and has no value:

<?php
$array = array();
var_dump($array); // 输出 array(0) { }
?>

2. Define an array containing elements

You can use the following method to initialize an array containing elements Array:

$array = array('a', 'b', 'c');

or

$array = ['a', 'b', 'c'];

Using the above code snippet, $array will be initialized to an array containing 3 elements, where each element corresponds to the array index starting from 0. of.

Output the above array:

<?php
$array = array('a', 'b', 'c');
print_r($array);
// array(
//    [0] => 'a',
//    [1] => 'b',
//    [2] => 'c'
// )
?>

3. Use key-value pairs to define the array

When the array element is more than just a word or number, you can use key-value pairs to define an array. array.

The following is a sample code:

$array = array(
    'name' => 'John',
    'age' => 30,
    'address' => 'New York'
);

or

$array = [
    'name' => 'John',
    'age' => 30,
    'address' => 'New York'
];

The above code is an associative array, each element has a key and corresponding value.

Output the above array:

 'John',
//     ['age'] => 30,
//     ['address'] => 'New York'
// )
?>

4. Use the range() function to create a numeric array

The range() function can be used to create a numeric array that contains a All elements in the specified range.

The following is a sample code:

<?php
$array = range(0, 5);

print_r($array);
// array(
//    [0] => 0,
//    [1] => 1,
//    [2] => 2,
//    [3] => 3,
//    [4] => 4,
//    [5] => 5
// )
?>

The above code will generate an array containing integers from 0 to 5.

5. Use the array_fill() function to initialize the array

The array_fill() function can be used to initialize all elements within the specified range in the array, as shown below:

$array = array_fill(0, 5, 'hello');

Above The code will generate an array containing 5 'hello' strings.

Output the above array:

<?php
$array = array_fill(0, 5, 'hello');
print_r($array);
// array(
//     [0] => 'hello',
//     [1] => 'hello',
//     [2] => 'hello',
//     [3] => 'hello',
//     [4] => 'hello',
// )
?>

6. Use the array_combine() function to create an associative array

The array_combine() function can be used to combine the values ​​​​in two arrays to create An associative array.

The following is a sample code:

$keys = array('a', 'b', 'c');
$values = array('x', 'y', 'z');

$array = array_combine($keys, $values);

Output the above array:

<?php
$keys = array('a', 'b', 'c');
$values = array('x', 'y', 'z');
$array = array_combine($keys, $values);
print_r($array);
// array(
//     ['a'] => 'x',
//     ['b'] => 'y',
//     ['c'] => 'z'
// )
?>

The above is a detailed introduction to the PHP array initialization method. By using different methods we can easily create the required array.

The above is the detailed content of There are several array initialization methods in php. What are they?. 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
Previous article:centos does not start phpNext article:centos does not start php