Reference to a one-dimensional array

Source: Internet
Author: User
Tags foreach explode sort

Basic syntax:

$ array name [key value];

Note: If you write a key value does not exist, it will count the group out of bounds error.

Several important functions of PHP array;

(1) Count: Counts the number of elements in an array

Format:

Count ($arr);

(2) Is_array: Determine whether a variable is an array. is an array, returns the true, otherwise, returns false.

Format:

Is_array ($arr);

(3) Print_r: Display array information

Format: Print_r ($arr);

(4) Var_dump: Display array information

Format: Var_dump ($arr);

(5) Explode: Splits a string, splits another string using one string, and splits the result into an array

Format:

Explode ("Divide by what", string);

Cases:

<?php
$STR = "Beijing Shanghai Tianjin Shenzhen Qingdao Guangzhou";
In actual development, involving the splitting of strings, you can consider
$arr =explode ("", $str); Split the $str with an empty partition, the result is an array, stored in an array $arr
Print_r ($arr);
?>

(6) Sort: for sorting

Format:

Sort ($arr);

Example: Use for and foreach to traverse the same array

<?php
$colors = Array (' Red ', ' blue ', ' green ', ' yellow ');
/*
* Iterate with For loop
for ($i =0; $i <count ($colors); $i + +) {
echo $colors [$i]. ' <br/> ';
} */
Traversing with foreach
foreach ($colors as $key => $var) {
Echo $var. "<br/>";
}
?>

By using unset to delete one of the key-value pairs of an array, the array will not rebuild the index.

Example: Deleting an array element

<?php
$arr [0]=123;
$arr [1]=456;
$arr [2]= "Hello";
echo "<br/> before deletion";
echo $arr [1];
Unset ($arr [1]);//delete $arr[1] Element
echo "<br/> deleted after <br/>";
echo $arr [1];
Print_r ($arr);
?>

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.