PHP Array function library array set up arrays

Source: Internet
Author: User
Tags array arrays integer
Functions | arrays

1. New array using array

Array array ([mixed ...])

Returns an array created from a parameter. Parameters can be indexed using the => operator. For information about what the array is, read the section of the array.

Note: Array () is a language structure that literally represents an array, not a regular function.

Syntax "Index => values", separated by commas, defines indexes and values. An index can be a string or a number. If an index is omitted, an integer index starting at 0 is automatically generated. If the index is an integer, the next resulting index will be the current largest integer index + 1. Note If two identical indexes are defined, the latter one overwrites the previous one.

Adding a comma after the last defined array item is not uncommon, but is a legitimate syntax.

The following example shows how to create a two-dimensional array, assign a key name to the corresponding array, and skip and continue the numeric index in the normal array. 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 ("A", 5 => "second", "third")
);
?>


Example 2. Automatic indexing of Array ()

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

The example above will output:

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


Note that index 3 is defined two times and retains the last value of 13. Index 4 is defined after index 8, and the next auto-generated index (with a value of 19) is 9, because the largest index is 8.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.