PHP/Data Structure/Array Element
Содержание
- 1 Add elements to the end of an array
- 2 An array called $dinner with numeric keys
- 3 Array and scalar collision
- 4 Array element navigation: next
- 5 Array element navigation: prev
- 6 Array index
- 7 Arrays Demo
- 8 Assign elements from array to variables
- 9 Assigning Array Values
- 10 Autogeneration of Array Indexes
- 11 Changing array values
- 12 Comparing Arrays: equality, identity, and the difference
- 13 Display an array
- 14 Elements of the enumerated array are numbers
- 15 Operating on array elements
- 16 Reference Array by index
- 17 Use the index in square brackets to create new elements and or assign values
Add elements to the end of an array
<?
$myarray = array("one", 2, "three");
$myarray[] = "the fourth element";
echo($myarray[3]);
?>
An array called $dinner with numeric keys
<?
$dinner[0] = "S";
$dinner[1] = "L";
$dinner[2] = "B";
?>
Array and scalar collision
<?
// This makes $vegetables an array
$vegetables["corn"] = "yellow";
// This makes $vegetables a scalar
$vegetables = "delicious";
// This makes $fruits a scalar
$fruits = 283;
// This makes $fruits an array and deletes its previous scalar value
$fruits["potassium"] = "banana";
?>
<?
$major_city_info = array();
$major_city_info[0] = "A";
$major_city_info["A"] = "B";
$major_city_info[1] = "C";
$major_city_info["C"] = "D";
$major_city_info[2] = "E";
$major_city_info["E"] = "F";
function print_all_array($city_array)
{
$current_item = current($city_array);
if ($current_item)
print("$current_item<BR>");
else
print("There"s nothing to print");
while($current_item = next($city_array))
print("$current_item<BR>");
}
print_all_array($major_city_info);
print_all_array($major_city_info);// again, to see what happens
?>
<?
$major_city_info = array();
$major_city_info[0] = "A";
$major_city_info["A"] = "B";
$major_city_info[1] = "C";
$major_city_info["C"] = "D";
$major_city_info[2] = "E";
$major_city_info["E"] = "F";
function print_all_array_backwards($city_array)
{
$current_item = end($city_array); //fast-forward to last
if ($current_item)
print("$current_item<BR>");
else
print("There"s nothing to print");
while($current_item = prev($city_array))
print("$current_item<BR>");
}
print_all_array_backwards($major_city_info);
?>
Array index
<?php
$arr = array();
$arr[0] = "First ";
$arr[1] = " PHP ";
$arr[2] = "array";
echo( $arr[0] . $arr[1] . $arr[2] );
?>
<hr>
<?php
$mo = array( "Jan ", "Feb ", "Mar " );
$dy = array( "21 ", "22 ", "23 " );
$yr = array( "2005", "2006", "2007" );
echo( $mo[1] . $dy[0] . $yr[1] );
?>
Arrays Demo
<?php
$weekdays[] = "Monday";
$weekdays[] = "Tuesday";
?>
<?php
$weekdays[0] = "Monday";
$weekdays[1] = "Tuesday";
?>
<?php
$weekdays[0] = "Monday";
$weekdays[1] = "Tuesday";
$weekdays[3] = "Wednesday";
?>
Assign elements from array to variables
<?php
$scores = array(88, 75);
@list($maths, $english, $history) = $scores;
@printf("<p>Maths: %d; English: %d; History: %d; Biology: %d.</p>\n", $maths, $english, $history, $biology);
?>
Assigning Array Values
<?php
function assign_key() {
return "d";
}
$foo["a"] = 1;
$foo["b"] = 2;
$foo["c"] = 3;
$foo[assign_key()] = 4; /* Assigned the key value "d" */
?>
Autogeneration of Array Indexes
<?php
$foo[] = "Value 1"; /* Assigned key 0 */
$foo[] = "Value 2"; /* Assigned key 1 */
$foo[5] = "Value 3"; /* Assigned key 5 */
$foo[] = "Value 4"; /* Assigned key 6 */
?>
Changing array values
<html>
<head>
<title>Changing array values</title>
</head>
<body>
<?php
$arr = array( "Red ", "Green ", "Blue" );
echo( $arr[0] . $arr[1] . $arr[2] . "<hr>" );
$arr[0] = 44;
$arr[1] = 12.5;
$arr[2] = $arr[0] + $arr[1];
echo( "$arr[0] + $arr[1] = $arr[2]" );
?>
</body>
</html>
Comparing Arrays: equality, identity, and the difference
<?php
function array_eq_ident($arr1, $arr2) {
printf("<p>The two arrays are %sequal.</p>\n", $arr1 == $arr2 ? "" : "not ");
printf("<p>The two arrays are %sidentical.</p>\n", $arr1 === $arr2 ? "" : "not ");
}
$dogs = array("A" => "C", "B" => "D", "X" => "Z", "Q" => "T");
$pups = array("A" => "C", "B" => "D", "X" => "Z", "Q" => "T");
array_eq_ident($dogs, $pups);
?>
Display an array
<?php
function array_display($array, $pre=FALSE)
{
$tag = $pre ? "pre" : "p";
printf("<%s>%s</%s>\n", $tag, var_export($array, TRUE), $tag);
}
function change(&$element, $key, $mark)
{
$element = "$mark$key$mark, the $element";
}
$dogs = array("A" => "AA", "Bud" => "BB");
array_display($dogs, TRUE);
array_walk($dogs, "change", "*");
array_display($dogs, TRUE);
?>
Elements of the enumerated array are numbers
<?php
$sortnumbers []= 10000;
$sortnumbers []= 10;
$sortnumbers []= 100;
$sortnumbers []= 1000;
$sortnumbers []= 1;
sort ($sortnumbers);
foreach ($sortnumbers as $val) {
echo "$val", "\n";
}
?>
Operating on array elements
<?
$dishes["A"] = 12;
$dishes["A"]++;
$dishes["B"] = 3;
$dishes["total"] = $dishes["A"] + $dishes["B"];
if ($dishes["total"] > 15) {
print "You ate a lot: ";
}
print "You ate " . $dishes["A"];
?>
Reference Array by index
<?
$myarray = array("one", 2, "three");
echo($myarray[0]);
echo($myarray[1]);
echo($myarray[2]);
?>
Use the index in square brackets to create new elements and or assign values
<?
$myarray = array("one", 2, "three");
$myarray[1] = "two";
$myarray[3] = "four";
echo($myarray[0]);
echo($myarray[1]);
echo($myarray[2]);
echo($myarray[3]);
?>