Use the array function in PHP to create an array _php instance

Source: Internet
Author: User

The array in PHP is actually an ordered map. A mapping is a type that associates values to the keys. This type is optimized in many ways, so you can think of it as a real array, or a list (vector), a hash list (an implementation of a map), a dictionary, a collection, a stack, a queue, and more possibilities. Because the value of an array element can also be another array, a tree structure and a multidimensional array are allowed.

Array

(PHP 4, PHP 5)

array-Create a new array

Description

Copy Code code as follows:

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"),
  "n Umbers "=> Array (1, 2, 3, 4, 5, 6),
  " holes "  => Array (" A ", 5 =>" second "," third ")
);
? >

Automatic indexing of Example #2 Array ()

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

The above routines 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.

This example establishes an array starting with 1.

Example #3 Array () indexed starting at 1

<?php
$firstquarter = Array (1 => ' January ', ' February ', ' March ');
Print_r ($firstquarter);
? >

The above routines will output:

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

In Perl, you can access the values of an array in double quotes. But in PHP you need to enclose the array in curly braces.

Example #4 access to an array within double quotes

<?php
$foo = Array (' Bar ' => ' Baz ');
echo "Hello {$foo [' Bar ']}!];" Hello baz!
? >

PHP Array function

PHP: Indicates the earliest version of PHP that supports this function.

function Description PHP
Array () Creates an array. 3
Array_change_key_case () Returns an array whose keys are either uppercase or lowercase. 4
Array_chunk () Divides an array into a new array block. 4
Array_combine () Creates a new array by merging two arrays. 5
Array_count_values () Used to count the occurrences of all values in an array. 4
Array_diff () Returns an array of difference sets for two arrays. 4
ARRAY_DIFF_ASSOC () Compares the key name and key values and returns an array of difference sets for two arrays. 4
Array_diff_key () Compares the key name and returns an array of difference sets for two arrays. 5
ARRAY_DIFF_UASSOC () Computes the difference set of an array by indexing the user-supplied callback function. 5
Array_diff_ukey () Compares the difference set of an array with a callback function for the key name. 5
Array_fill () Fills the array with the given value. 4
Array_filter () Filters the elements in an array with a callback function. 4
Array_flip () Swap the keys and values in the array. 4
Array_intersect () Computes the intersection of an array. 4
ARRAY_INTERSECT_ASSOC () Compares the key name and key values and returns an array of intersections of two arrays. 4
Array_intersect_key () Computes the intersection of an array by using the key name comparison. 5
ARRAY_INTERSECT_UASSOC () Computes the intersection of an array with an index check and compares the index with a callback function. 5
Array_intersect_ukey () Computes the intersection of the array using the callback function to compare the key name. 5
Array_key_exists () Checks whether the given key name or index exists in the array. 4
Array_keys () Returns all the key names in the array. 4
Array_map () The callback function is acting on the cell of the given array. 4
Array_merge () Merges one or more arrays into an array. 4
Array_merge_recursive () Recursively merge one or more arrays. 4
Array_multisort () Sorts multiple arrays or multidimensional arrays. 4
Array_pad () Fills an array with a value to a specified length. 4
Array_pop () Eject the last cell of the array (out of the stack). 4
Array_product () Calculates the product of all the values in an array. 5
Array_push () Presses one or more units (elements) into the end of the array (into the stack). 4
Array_rand () Randomly selects one or more elements from an array and returns. 4
Array_reduce () Iteratively simplifies the array to a single value using the callback function. 4
Array_reverse () Flips the order of elements in the original array, creates a new array, and returns. 4
Array_search () Searches the array for a given value, and returns the corresponding key name if successful. 4
Array_shift () Deletes the first element in the array and returns the value of the deleted element. 4
Array_slice () Takes a value out of a condition in an array and returns it. 4
Array_splice () Remove a portion of the array and replace it with other values. 4
Array_sum () Calculates the and of all the values in the array. 4
Array_udiff () Use the callback function to compare data to compute the difference set of an array. 5
ARRAY_UDIFF_ASSOC () Calculates the difference set of an array with an index check and compares the data with a callback function. 5
ARRAY_UDIFF_UASSOC () Calculates the difference set of an array with an index check and compares the data and index with the callback function. 5
Array_uintersect () Computes the intersection of the array and compares the data with the callback function. 5
ARRAY_UINTERSECT_ASSOC () Computes the intersection of the array with the index check and compares the data with the callback function. 5
ARRAY_UINTERSECT_UASSOC () Computes the intersection of the array with the index check and compares the data and index with the callback function. 5
Array_unique () Deletes the duplicate value in the array. 4
Array_unshift () Inserts one or more elements at the beginning of the array. 4
Array_values () Returns all the values in the array. 4
Array_walk () Apply a user function to each member in the array. 3
Array_walk_recursive () Each member of an array applies a user function recursively. 5
Arsort () Reverse-Sort the array and keep the index relationship. 3
Asort () The array is sorted and the index relationship is maintained. 3
Compact () Create an array that includes the variable names and their values. 4
Count () Calculates the number of elements in an array or the number of attributes in an object. 3
Current () Returns the current element in an array. 3
each () Returns the current key/value pair in the array and moves the array pointer one step forward. 3
End () Points the array's internal pointer to the last element. 3
Extract () Imports a variable from an array into the current symbol table. 3
In_array () Checks whether the specified value exists in the array. 4
Key () Gets the key name from the associative array. 3
Krsort () The arrays are sorted in reverse order by key names. 3
Ksort () The array is sorted by key name. 3
List () Assign the values in the array to some variables. 3
Natcasesort () Use the "natural sort" algorithm to sort the case-insensitive letters of the array. 4
Natsort () Sort the array using the natural sort algorithm. 4
Next () Moves the internal pointer in the array forward one bit. 3
POS () The alias for current (). 3
Prev () Returns the internal pointer of an array to a bit. 3
Range () Creates an array that contains the elements of the specified range. 3
Reset () Points the array's internal pointer to the first element. 3
Rsort () Reverse-Sort the array. 3
Shuffle () Rearrange the elements in the array in random order. 3
sizeof () The alias for Count (). 3
Sort () Sort the array. 3
Uasort () Use the user-defined comparison function to sort the values in the array and keep the index associated. 3
Uksort () Use the user-defined comparison function to sort the key names in the array. 3
Usort () Use the user-defined comparison function to sort the values in the array. 3

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.