PHP/Data Structure/Array Function

Материал из Web эксперт
Версия от 10:02, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Array array_chunk

   <source lang="html4strict">

<?php

  $cards = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P");
  // shuffle the cards
  print_r($cards);
  
  print "


"; shuffle($cards); // Use array_chunk() to divide the cards into four equal "hands" $hands = array_chunk($cards, 4); print_r($hands);

?>


      </source>
   
  


array_combine: Creates an array by using one array for keys and another for its values

   <source lang="html4strict">

<?php

  $abbreviations = array("AL","AK","AZ","AR");
  $states = array("Alabama","Alaska","Arizona","Arkansas");
  $stateMap = array_combine($abbreviations,$states);
  print_r($stateMap);

?>

      </source>
   
  


Array count value

   <source lang="html4strict">

<?php

  $states = array("Ohio","Iowa","Arizona","Iowa","Ohio");
  $stateFrequency = array_count_values($states);
  print_r($stateFrequency);

?>

      </source>
   
  


array_diff_assoc: Array difference for associate array

   <source lang="html4strict">

<?php

  $array1 = array("OH" => "Ohio", "CA" => "California", "HI" => "Hawaii");
  $array2 = array("50" => "Hawaii", "CA" => "California", "OH" => "Ohio");
  $array3 = array("TX" => "Texas", "MD" => "Maryland", "KS" => "Kansas");
  $diff = array_diff_assoc($array1, $array2, $array3);
  print_r($diff);

?>

      </source>
   
  


array_flip: Exchanges all keys with their associated values in an array

   <source lang="html4strict">

<?php

  $state = array("Delaware","Pennsylvania","New Jersey");
  $state = array_flip($state);
  print_r($state);
  // Array ( [Delaware] => 0 [Pennsylvania] => 1 [New Jersey] => 2 )

?>

      </source>
   
  


array_merge_recursive: Merge two or more arrays recursively

   <source lang="html4strict">

<?php

  $class1 = array("A" => 100, "C" => 85);
  $class2 = array("B" => 78, "D" => 45);
  $classScores = array_merge_recursive($class1, $class2);
  print_r($classScores);

?>


      </source>
   
  


array_multisort: Sort multiple or multi-dimensional arrays

   <source lang="html4strict">

<?php

  $staff["givenname"][0] = "A";
  $staff["givenname"][1] = "B";
  $staff["givenname"][2] = "C";
  $staff["givenname"][3] = "D";
  $staff["surname"][0] = "E";
  $staff["surname"][1] = "F";
  $staff["surname"][2] = "G";
  $staff["surname"][3] = "H";
  $res = array_multisort($staff["surname"],SORT_STRING,SORT_ASC,$staff["givenname"],SORT_STRING,SORT_ASC);
  print_r($staff);

?>

      </source>
   
  


array_pad: Pad array to the specified length with a value

   <source lang="html4strict">

<?php

  $states = array("A","B");
  print "before pad:
"; print_r($states); $states = array_pad($states,4,"C"); $states = array("A","B","C","D"); print "
after pad:
"; print_r($states);

?>

      </source>
   
  


array_rand: Pick one or more random entries out of an array

   <source lang="html4strict">

<?php

  $states = array("Ohio" => "Columbus", "Iowa" => "Des Moines","Arizona" => "Phoenix");
  $randomStates = array_rand($states, 2);
  print_r($randomStates);

?>

      </source>
   
  


array_reverse: Return an array with elements in reverse order

   <source lang="html4strict">

<?php

  $states = array("A","B","C");
  print "Before reverse: 
"; print_r($states); print "
After reverse:
"; print_r(array_reverse($states));

?>

      </source>
   
  


array_search: Searches the array for a given value and returns the corresponding key if successful

   <source lang="html4strict">

<?php

  $state["Ohio"] = "March 1";
  $state["Delaware"] = "December 7";
  $state["Pennsylvania"] = "December 12";
  $founded = array_search("December 7", $state);
  if ($founded) echo "The state $founded was founded on $state[$founded]";

?>

      </source>
   
  


Array shuffle

   <source lang="html4strict">

<?php

  $cards = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P");
  // shuffle the cards
  print_r($cards);
  
  print "


"; shuffle($cards); // Use array_chunk() to divide the cards into four equal "hands" $hands = array_chunk($cards, 4); print_r($hands);

?>


      </source>
   
  


array_splice: Remove a portion of the array and replace it with something else

   <source lang="html4strict">

<?php

  $states = array("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Connecticut");
  $subset = array_splice($states, 2, -1, array("New York", "Florida"));
  print_r($states);

?>

      </source>
   
  


array_sum: Calculate the sum of values in an array

   <source lang="html4strict">

<?php

  $grades = array(42,"hello",42);
  $total = array_sum($grades);
  print $total;

?>


      </source>
   
  


array_unshift

   <source lang="html4strict">

<?php $states = array("A","B"); print "before unshift:
"; print_r($states); array_unshift($states,"D","E"); print "
after unshift:
"; print_r($states); ?>

      </source>
   
  


count() function can also be used to access certain elements in the array

   <source lang="html4strict">
<?php
 $employee []= "A";
 $employee []= "B";
 $employee []= "C";
 $employee []= "D";
 echo $employee [count ($employee)-1];
?>
          
      </source>
   
  


current: Return the current element in an array

   <source lang="html4strict">

<?php $fruits = array("apple", "orange", "banana"); print_r($fruits); $fruit = current($fruits); // returns "apple" echo "Fruit: $fruit
"; $fruit = next($fruits); // returns "orange" echo "Fruit: $fruit
"; $fruit = prev($fruits); // returns "apple" echo "Fruit: $fruit
"; ?>

      </source>
   
  


end: Set the internal pointer of an array to its last element

   <source lang="html4strict">

<?php $fruits = array("apple", "orange", "banana"); print_r($fruits); $fruit = current($fruits); // returns "apple" echo "Current: $fruit
"; $fruit = end($fruits); // returns "banana" echo "End: $fruit
"; ?>

      </source>
   
  


in_array: Checks if a value exists in an array

   <source lang="html4strict">

<?php

  $grades = array(100,94.7,67,89,100);
  if (in_array("100",$grades)) 
     echo "Somebody studied for the test!";
  
  if (in_array("100",$grades,1)) 
     echo "Somebody studied for the test!";

?>

      </source>
   
  


next: Advance the internal array pointer of an array

   <source lang="html4strict">

<?php

  $fruits = array("apple", "orange", "banana");
  print_r($fruits);
  $fruit = next($fruits); // returns "orange"
  echo "Next fruit: $fruit
"; $fruit = next($fruits); // returns "banana" echo "Next fruit: $fruit
";

?>

      </source>
   
  


The count() Function

   <source lang="html4strict">

<?php

 $employee []= "A";
 $employee []= "B";
 $employee []= "C";
 $employee []= "D";
 echo count ($employee);

?>

      </source>
   
  


Using the array_diff () Function

   <source lang="html4strict">
<?php
 $arrayone = array ("A", "B", "C", "D");
 $arraytwo = array ("C", "D", "E", "F");
 $diff = array_diff ($arrayone, $arraytwo);
 foreach ($diff as $val) {
    echo "$val", "\n";
 }
?>
          
      </source>
   
  


Using the array_intersect () Function

   <source lang="html4strict">
<?php
 $arrayone = array ("A", "B", "C", "D");
 $arraytwo = array ("C", "D", "E", "F");

 $similarity = array_intersect ($arrayone, $arraytwo);
 foreach ($similarity as $val) {
    echo "$val", "\n";
 }
?>
          
      </source>
   
  


Using the array_merge () Function

   <source lang="html4strict">
 <?php
 $arrayA = array ("A", "B", "C", "D", "E");
 $arrayB = array (1, 2, 3, 4);
 $arrayAB = array_merge ($arrayA, $arrayB);
 echo "The number of elements in \$arrayAB are: ", "\n";
 foreach ($arrayAB as $val) {
   echo "$val", "\n";
 }
 ?>
          
      </source>
   
  


Using the array_push () Function

   <source lang="html4strict">
<?php
 $vocalists = array ("A", "B", "C");
 $musicians = array_push($vocalists, "D", "E", "F");
 echo "The total number of musicians are $musicians:", "\n";
 foreach ($vocalists as $val) {
   echo "$val", "\n";
 }
 ?>
          
      </source>
   
  


Using_the array_shift () Function

   <source lang="html4strict">
<?php
 $music = array ("A", "B", "C", "D");
 while (count ($music)) {
    $somevalue = array_shift ($music);
    echo "$somevalue", "
"; echo count ($music); echo "
"; }  ?> </source>


Using the array_slice () Function

   <source lang="html4strict">

<?php

 $musicgenre = array ("A","B","C","D","E","F","G");
 echo "array_slice ($musicgenre, 4, 2):", "
"; $Dave = array_slice ($musicgenre, 4, 2); foreach ($Dave as $valone) { echo "$valone", "
"; } echo "
"; echo "array_slice ($musicgenre, 1, 3):", "
"; $Brad = array_slice ($musicgenre, 1, 3); foreach ($Brad as $valtwo) { echo "$valtwo", "
"; } echo "
"; echo "array_slice ($musicgenre, 3, 3):", "
"; $Joe = array_slice ($musicgenre, 3, 3); foreach ($Joe as $valthree) { echo "$valthree", "
"; } echo "
"; echo "array_slice ($musicgenre, -4, 2):", "
"; $Rick = array_slice ($musicgenre, -4, 2); foreach ($Rick as $valfour) { echo "$valfour", "
"; } echo "
"; echo "array_slice ($musicgenre, 1):", "
"; $John = array_slice ($musicgenre, 1); foreach ($John as $valfive) { echo "$valfive", "
"; }

?>

      </source>
   
  


Using the array_unique () Function

   <source lang="html4strict">

<?php

 $oldarray = array ("A", "B", "C", "D", "E", "F");
 $newarray = array_unique ($oldarray);
 
 foreach ($newarray as $val) {
    echo "$val", "\n";
 }

?>

      </source>