PHP Basics Summary--arrays

Source: Internet
Author: User
Tags php basics sorts

Indexed array: An array with a numeric index

Initializing an array

$arr Array (' value1 ', ' value2 ', ' value3 ');  $arr = [' value1 ', ' value2 ', ' value3 ']; /* PHP5.4 version starts to support */

Iterating through an array

For loop traversal:

 for ($i$i<count($arr$i+ +)    {echo$ Arr[$i]. ' ';}

foreach Loop traversal:

foreach ($arras$value) {    echo$value. ' ';}

Array sorting

Sort    ($arr) /* sort the indexed array in ascending order */ Rsort    ($arr) /* sort the indexed array in descending order */ /* when these two functions are used for associative arrays, the key values are ignored to sort the associative array into an indexed array */

Associative arrays: Arrays of custom key values

Initializing an array

$arr Array (' key1 ' = ' value1 ', ' key2 ' = ' value2 ', ' key3 ' = ' value3 ');  $arr = [' key1 ' = ' value1 ', ' key2 ' = ' value2 ', ' Key3 ' and ' value3 ']; /* PHP5.4 version starts to support */

Iterating through an array

An indexed array cannot be traversed with a for loop (no numeric index exists), but it can still be used with foreach:

foreach ($arras$key= +$value) {  echo$key. ' = '. $value. " ;}

There is also a cool way to traverse:

 while (list($key,$valueeach ($arr)) {    echo$key . ' = '. $value. " ;}

Array sorting

Asort($arr);/*sorts in ascending order based on the values of the array. */Ksort($arr);/*Sort Ascending by the keys of the array. */Arsort($arr);/*sorts in descending order based on the values of the array. */Krsort($arr);/*sorts the keys of the array in descending order. */  /*Ksort (), Aksort () Two functions are not valid for indexed arrays, Asort (), Arsort () two functions are equivalent to sort (), Rsort () for an indexed array*/

Array Common functions

Count();/*returns the array length (number of elements)*/ Array_pop();/*Delete the last element of the array (out of the stack)*/ Array_push();/*inserts one or more elements into the end of an array (into the stack)*/Array_merge();/*combine one or more arrays into an array*/Array_unique();/*Remove the same element from the array*/In_array();/*detects if a value is in an array (returns True and false)*/Shuffle();/*disturb the original array*/...

PHP Basics Summary--arrays

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.