Php Array

*

Bạn đang xem: Php array


5.3. Storing Data in Arrays

Storing a value in an array will createthe array if it didn"t already exist, but trying toretrieve a value from an array that hasn"t beendefined yet won"t create the array. For example:

// $addresses not defined before this pointecho $addresses<0>; // prints nothingecho $addresses; // prints nothing$addresses<0> = "spam
cyberpromo.net";echo $addresses; // prints "Array"Using simple assignmentto initialize an array in your program leads khổng lồ codelike this:

$addresses<0> = "spam
example.com";// ...That"s an indexed array, with integer indexesbeginning at 0. Here"s anassociative array:

$price<"Gasket"> = 15.29;$price<"Wheel"> = 75.25;$price<"Tire"> = 50.00;// ...An easier way to lớn initialize an array is khổng lồ use the array() construct, which builds an array fromits arguments:

$addresses = array("spam
example.com");To create an associative array with array( ), usethe => symbol to lớn separate indexes fromvalues:

$price = array("Gasket" => 15.29, "Wheel" => 75.25, "Tire" => 50.00);Notice the use of whitespace & alignment. We could have bunched upthe code, but it wouldn"t have been as easy to lớn read:

$price = array("Gasket"=>15.29,"Wheel"=>75.25,"Tire"=>50.00);To construct an empty array, pass no arguments toarray( ):

$addresses = array( );You can specify an initial key with => và thena các mục of values. The values are inserted into the array startingwith that key, with subsequent values having sequential keys:

$days = array(1 => "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");// 2 is Tuesday, 3 is Wednesday, etc.If the initial index is a non-numeric string, subsequent indexes areintegers beginning at 0. Thus, the following code is probably amistake:

$whoops = array("Friday" => "Black", "Brown", "Green");// same as$whoops = array("Friday" => "Black", 0 => "Brown", 1 => "Green");

Xem thêm: Những Bài Thuốc Dân Gian Chữa Bệnh Viêm Loét Dạ Dày, 7 Cách Chữa Đau Dạ Dày Tại Nhà Mẹo Dân Gian

5.3.1. Adding Values khổng lồ the kết thúc of an Array

to insert more values into the kết thúc of anexisting indexed array, use the <> syntax:

$family = array("Fred", "Wilma");$family<> = "Pebbles"; // $family<2> is "Pebbles"This construct assumes the array"s indexes arenumbers and assigns elements into the next available numeric index,starting from 0. Attempting to lớn append to lớn an associative array isalmost always a programmer mistake, but PHP will give the newelements numeric indexes without issuing a warning:

$person = array("name" => "Fred");$person<> = "Wilma"; // $person<0> is now "Wilma"

5.3.2. Assigning a Range of Values

The range( ) function creates an array ofconsecutive integer or character values between the two values youpass lớn it as arguments. For example:

$numbers = range(2, 5); // $numbers = array(2, 3, 4, 5);$letters = range("a", "z"); // $numbers holds the alphabet$reversed_numbers = range(5, 2); // $numbers = array(5, 4, 3, 2);Only the first letter of a string argument is used lớn build the range:

range("aaa", "zzz") /// same as range("a","z")

5.3.3. Getting the form size of an Array

The count( ) & sizeof( )functions are identical in use and effect. They return the number ofelements in the array. There is no stylistic preference about whichfunction you use. Here"s an example:

$family = array("Fred", "Wilma", "Pebbles");$size = count($family); // $size is 3These functions vày not consult any numeric indexes that might bepresent:

$confusion = array( 10 => "ten", 11 => "eleven", 12 => "twelve");$size = count($confusion); // $size is 3

5.3.4. Padding an Array

To create an arrayinitialized khổng lồ the same value, use array_pad( ).The first argument khổng lồ array_pad() is the array, the second argument isthe minimum number of elements you want the array khổng lồ have, và thethird argument is the value to give any elements that are created.The array_pad( ) function returns a new paddedarray, leaving its argument array alone.

Here"s array_pad( ) in action:

$scores = array(5, 10);$padded = array_pad($scores, 5, 0); // $padded is now array(5, 10, 0, 0, 0)Notice how the new values are appended to the end of the array. Ifyou want the new values added khổng lồ the start of the array, use anegative second argument:

$padded = array_pad($scores, -5, 0);Assign the results of array_pad( ) back lớn theoriginal array lớn get the effect of an in situ change:

$scores = array_pad($scores, 5, 0);If you pad an associativearray, existing keys will be preserved. New elements will havenumeric keys starting at 0.


*
5.2. Identifying Elements of an Array
*
5.4. Multidimensional Arrays

*