An array of PHP

Source: Internet
Author: User
Tags http cookie http post pear

Array classification:

1. Index Array. The index value starts at 0 and increments sequentially.

2. Declaring an array using the array () function

<?php    //1. Assign a value directly to an array element to declare it    $contact _index[0] = 1;    $contact _index[1] = "Gao MoU";    $contact _index[2] = "a company";    $contact _index[3] = "Beijing";    $contact _index[] = "[email protected]";    Var_dump ($contact _index);    $contact _key["ID"] = "2";    $contact _key["name"] = "peak";    $contact _key["Company"] = "Company B";    $contact _key["email"] = "[email protected]";    Var_dump ($contact _key);    2. Declare an array using the array () function    $contact _key_array = Array (        "ID" = 1;        " Name "= =" Peak One ";        " Company "=" b Company ";        " Email "=" [email protected];    );    Var_dump ($contact _key_array);? >

Traversal of the array:

1.for Statement Traversal array

2.foreach Statement Traversal array

<?php    //Use the For statement to iterate through the array    $cars =array ("Volvo", "BMW", "SAAB");    $arrlength =count ($cars);    for ($i =0; $i < $arrlength; $i + +) {    echo $cars [$i];    echo "<br>";    }   ? ><?php
The foreach statement iterates through the associative array $age =array ("Bill" = "+", "steve" = "Notoginseng", "Peter" and "a"); foreach ($age as $x = + $x _value) { echo "key=". $x. ", value=. $x _value; echo "<br>"; }? >

Pre-defined arrays

1. It is a special array, no difference in operation mode.

2. Do not declare them, and each PHP script has a default.

3. They take effect automatically in the global scope.

Pre-defined array description

The $_server variable is set by the Web server or is directly associated with the execution environment of the current script

$_env the execution environment commits the variables to the script

$_get a variable submitted to the script via a URL request

$_post the variables submitted to the specimen via the HTTP POST method

$_request variables submitted to the script via the Get,post,cookie mechanism

$_files variables submitted to the script via HTTP POST file upload

$_cookie variables submitted to the script via the HTTP COOKIE method

$_session variables currently registered to the script session

$_globals contains a valid variable that refers to the global scope of each current script, and the key name of the array is the name of the global variable.

Merging arrays

The Array_merge () function merges the arrays together to return an array of unions.

Array Array_merge (array array1 array2.....arrayn)

<?php    $fruits =array ("Apaple", "banana", "pear");    $numbered =array ("1", "2", "3");    $cards =array_merge ($fruits, $numbered);    

Array ([0] = apaple [1] = banana [2] = pear [3] = 1 [4] = 2 [5] = 3)
?>

Splitting a fraction group

The Array_slice () function returns a portion of the array, starting with the key offset and ending at the offset+length position

Array array_slice (array array,int offset,[int length])

Offset is positive when starting from the front and negative when starting from the back

<?php    $fruits = Array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "watermelon");    $subset =array_slice ($fruits, 3);    Print_r ($subset);
Output
Array ([0] = Pear [1] = Grape [2] = Lemon [3] = watermelon)?>

<?php
$fruits = Array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "watermelon");
$subset =array_slice ($fruits, 2,-2);
Print_r ($subset);
Output
Array ([0] = Orange [1] = Pear [2] = Grape)
?>

Array header add element:

The Array_unshift function adds elements to the array header

<? PHP   $fruits = Array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon");   Array_unshift ($fruits, "AAAA", "BBBB");   Print_r ($fruits);//output//array ([0] = AAAA [1] = BBBB [2] = Apple [3] = = Banana [4] = Orange [5] =&G T Pear [6] = Grape [7] = Lemon)?>

Array header Delete element:

Array_shift Deleting an array header element

<?php   $fruits = Array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon");   Array_shift ($fruits);   Print_r ($fruits);//output//array ([0] = Banana [1] = Orange [2] = = Pear [3] = = Grape [4] = Lemon)? ;

Add elements at the end of the array:

The Array_push function adds elements at the end of the array

<?php    $fruits = Array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon");    Array_push ($fruits, "AAAA", "BBBB");    Print_r ($fruits);//output//array ([0] = Apple [1] = Banana [2] = Orange [3] = Pear [4] = = Grape [5] =& Gt Lemon [6] = AAAA [7] = BBBB)?>

The trailing element of the array is deleted:

The Array_pop function deletes the trailing element of an array, returning the last element of the array.

<?php   $fruits = Array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon");   $result =array_pop ($fruits);   Print_r ($result);   echo "<br>";   Print_r ($fruits);//output//$result  returns lemon//$fruits  returns an array ([0] = Apple [1] = Banana [2] = Orange [ 3] = Pear [4] = Grape)?>

An array of PHP

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.