Home  >  Article  >  Backend Development  >  Detailed explanation of using php array function to create an array

Detailed explanation of using php array function to create an array

怪我咯
怪我咯Original
2017-07-14 11:00:431645browse

PHP supports single-dimensional and multi-dimensional arrays. At the same time, it provides a function to construct an array using the databasequery results. The array function is a core component of PHP. This article introduces how to use the array function to create a new array in PHP. Friends who need it can refer to it

An array in PHP is actually an ordered map. A map is a type that associates values ​​to keys. This type is optimized in many ways, so it can be treated as a real array, or list (vector), hash table (which is an implementation of map), dictionary, set, stack, queue and many more possibilities. Since the value of an array element can also be another array, tree structures and multidimensional arrays are also allowed.

array

(PHP 4, PHP 5)

array — Create a new array

Description

array array ([ mixed $... ] )

Returns the array created based on the parameters. Parameters can be given index using the => operator. For information on what an array is please read the Arrays section.

Note:

array() is a language structure used to represent arrays literally, not a regular function.

The syntax "index => values", separated by commas, defines the index and value. The index can be a string or a number. If the index is omitted, an integer index starting from 0 is automatically generated. If the index is an integer, the next resulting index will be the largest integer index so far + 1. Note that if two identical indexes are defined, the latter one will overwrite the previous one.

Although uncommon, adding a comma after the last defined array item is legal syntax.

The following example demonstrates how to create a two-dimensional array, how to assign key names to the corresponding array, and how to skip and continue numeric indexes in ordinary arrays.

Example #1 array() example

<?php
$fruits = array (
  "fruits" => array("a" => "orange", "b" => "banana", "c" => "apple"),
  "numbers" => array(1, 2, 3, 4, 5, 6),
  "holes"  => array("first", 5 => "second", "third")
);
?>

Example #2 Automatic indexing of array()

<?php
$array = array(1, 1, 1, 1, 1, 8 => 1, 4 => 1, 19, 3 => 13);
print_r($array);
?>

Above example The process will output:

Array
(
[0] => 1
[1] => 1
[2] => 1
[3 ] => 13
[4] => 1
[8] => 1
[9] => 19
)

Note that index 3 is defined twice, retaining the last value 13. Index 4 is defined after index 8, and the next automatically generated index (the one with value 19) is 9 because the largest index is 8.

This example creates an array starting from 1.

Example #3 Array() indexed from 1

<?php
$firstquarter = array(1 => &#39;January&#39;, &#39;February&#39;, &#39;March&#39;);
print_r($firstquarter);
?>

The above routine will output:

 Array
(
  [1] => January
  [2] => February
  [3] => March
)

In Perl, you can access the array within double quotes value. But in PHP, arrays need to be enclosed in curly braces.

Example #4 Accessing an array within double quotes

<?php
$foo = array(&#39;bar&#39; => &#39;baz&#39;);
echo "Hello {$foo[&#39;bar&#39;]}!"; // Hello baz!
?>

The above is the detailed content of Detailed explanation of using php array function to create an array. 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