PHP Learning notes Array 1

Source: Internet
Author: User
1, generally $name[' Tom ' and $name[tom] are the same; but the key without the quotation mark cannot be distinguished from the constant, such as: Define (' Index ', 5); $name [' Tom '] and $name[tom] are different

2, the variable in double quotes is generally used in {} is a good habit, such as: Echo "{$name}";

3. Array $name = Array (' Tom ', ' Jim ', ' Alice '); and $username = [' Tom ', ' Jim ', ' Alice '];

4, the array with the = symbol to specify an initial value, followed by the key value is continuous;

PS1: $days = Array (1 = "Mon", "Tue", "Wed", "Thu"); The key value for the Tue is 2,wed with a key value of 3, and so on.

PS2: $days = Array (' One ' = "Mon", "Tue", "Wed", "Thu"); The key value of array 2 is 0, and the key value of array 3 is 1

5, the specified range: range ();

PS1: $num = Range (2, 5); $num = Array (2, 3, 4, 5);

PS2: $str = range (A, z); Save 26 letters A-Z;

PS3: $num = Range (5, 2); $num = Array (5, 4, 3, 2);

PS4: $str = Range ("abd", "ZDS"); Save A-Z of 26 letters, only the first character range, the number is also

6. Get the size of the array

The count () and sizeof () functions are identical in usage and effect;

such as $family = Array ("Tom", "Jim", "Cherry"); $size = count ($family); $size is 3.

7. Populating arrays

Array_pad () creates an array with duplicate content, the first parameter is an array, the second parameter is the minimum number of elements you want the array to have, the third argument is the initial value of each element, returns a new array, discards the source array

PS1:

$scores = Array (5, 10);

$padded = Array_pad ($scores, 5, 0); $padded is now array (5, 10, 0, 0, 0);

PS2:

$padded = Array_pad ($scores,-5, 0); $padded is now array (0, 0, 0, 5, 10);

8. Multidimensional arrays

To use variable substitution to access elements in a multidimensional array, you must enclose the entire element in curly braces:

echo "{$num [1][0]}"; Result is 4

echo "$num [1][0]"; Results for Array[0]

9. Extract Multiple values

To copy all the values in an array into a variable, you can use the list () structure;

$person = Arrary ("Tom", "Alice");

List ($name, $age, $wife) = $person; $name is Tom, $age is $wift Alice.

The list () function is typically used to extract data from a database query that returns only one record, which automatically assigns simple poor Uxnde data to a series of local variables. )

PS: $sql = "SELECT name, age, wife from the person where id = 2";

$result = mysql_query ($sql);

List ($name, $age, $wife) = Mysql_fetch_assoc ($result);

PS2: If the number of elements in the array is greater than the number of variables in list (), the extra value is ignored, and if the value in list () is more than the number in the array, the extra value is set to null;

Use more than two consecutive commas in the list () to skip the values in the array;

$values = Range (' A ', ' e ');

List ($m,, $n,, $o) = $values; $m is "a", $n "C", $o "E"

  • Related Article

    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.