PHP/Data Structure/Array Element

Материал из Web эксперт
Перейти к: навигация, поиск

Add elements to the end of an array

   <source lang="html4strict">

<? $myarray = array("one", 2, "three"); $myarray[] = "the fourth element"; echo($myarray[3]); ?>

      </source>
   
  


An array called $dinner with numeric keys

   <source lang="html4strict">

<? $dinner[0] = "S"; $dinner[1] = "L"; $dinner[2] = "B"; ?>

 </source>
   
  


Array and scalar collision

   <source lang="html4strict">

<? // 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"; ?>

 </source>
   
  


Array element navigation: next

   <source lang="html4strict">

<? $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
"); else print("There"s nothing to print"); while($current_item = next($city_array)) print("$current_item
");

} print_all_array($major_city_info); print_all_array($major_city_info);// again, to see what happens ?>

      </source>
   
  


Array element navigation: prev

   <source lang="html4strict">

<? $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
"); else print("There"s nothing to print"); while($current_item = prev($city_array)) print("$current_item
");

} print_all_array_backwards($major_city_info); ?>

      </source>
   
  


Array index

   <source lang="html4strict">

<?php

 $arr = array();
 $arr[0] = "First ";
 $arr[1] = " PHP ";
 $arr[2] = "array";
 echo( $arr[0] . $arr[1] . $arr[2] );

?>


<?php

 $mo = array( "Jan ", "Feb ", "Mar " );
 $dy = array( "21 ", "22 ", "23 " );
 $yr = array( "2005", "2006", "2007" );
   
 echo( $mo[1] . $dy[0] . $yr[1] );

?>

 </source>
   
  


Arrays Demo

   <source lang="html4strict">

<?php $weekdays[] = "Monday"; $weekdays[] = "Tuesday"; ?> <?php $weekdays[0] = "Monday"; $weekdays[1] = "Tuesday"; ?> <?php $weekdays[0] = "Monday"; $weekdays[1] = "Tuesday"; $weekdays[3] = "Wednesday"; ?>

 </source>
   
  


Assign elements from array to variables

   <source lang="html4strict">

<?php $scores = array(88, 75); @list($maths, $english, $history) = $scores;

@printf("

Maths: %d; English: %d; History: %d; Biology: %d.

\n", $maths, $english, $history, $biology);

?>

 </source>
   
  


Assigning Array Values

   <source lang="html4strict">

<?php

   function assign_key() {
       return "d";
   }
   $foo["a"] = 1;
   $foo["b"] = 2;
   $foo["c"] = 3;
   $foo[assign_key()] = 4;    /* Assigned the key value "d" */

?>

 </source>
   
  


Autogeneration of Array Indexes

   <source lang="html4strict">

<?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 */

?>

 </source>
   
  


Changing array values

   <source lang="html4strict">

<html>

<head>
 <title>Changing array values</title>
</head>
<body>
<?php
 $arr = array( "Red ", "Green ", "Blue" );
echo( $arr[0] . $arr[1] . $arr[2] . "
" );
 $arr[0] = 44;
 $arr[1] = 12.5;
 $arr[2] = $arr[0] + $arr[1];
   
 echo( "$arr[0] + $arr[1] = $arr[2]" );
?>
</body>

</html>

 </source>
   
  


Comparing Arrays: equality, identity, and the difference

   <source lang="html4strict">

<?php function array_eq_ident($arr1, $arr2) {

printf("

The two arrays are %sequal.

\n", $arr1 == $arr2 ? "" : "not "); printf("

The two arrays are %sidentical.

\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); ?>

 </source>
   
  


Display an array

   <source lang="html4strict">

<?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);

?>

 </source>
   
  


Elements of the enumerated array are numbers

   <source lang="html4strict">
<?php
 $sortnumbers []= 10000;
 $sortnumbers []= 10;
 $sortnumbers []= 100;
 $sortnumbers []= 1000;
 $sortnumbers []= 1;
 sort ($sortnumbers);
 foreach ($sortnumbers as $val) {
   echo "$val", "\n";
 }
?>
          
      </source>
   
  


Operating on array elements

   <source lang="html4strict">

<? $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"]; ?>

 </source>
   
  


Reference Array by index

   <source lang="html4strict">

<? $myarray = array("one", 2, "three"); echo($myarray[0]); echo($myarray[1]); echo($myarray[2]);

?>

      </source>
   
  


Use the index in square brackets to create new elements and or assign values

   <source lang="html4strict">

<? $myarray = array("one", 2, "three"); $myarray[1] = "two"; $myarray[3] = "four"; echo($myarray[0]); echo($myarray[1]); echo($myarray[2]); echo($myarray[3]); ?>

      </source>