PHP/Data Structure/Array Function

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

Array array_chunk

<?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 "<BR><BR><BR>";   
   shuffle($cards);
   // Use array_chunk() to divide the cards into four equal "hands"
   $hands = array_chunk($cards, 4);
   print_r($hands);
?>



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

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



Array count value

<?php
   $states = array("Ohio","Iowa","Arizona","Iowa","Ohio");
   $stateFrequency = array_count_values($states);
   print_r($stateFrequency);
?>



array_diff_assoc: Array difference for associate array

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



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

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



array_merge_recursive: Merge two or more arrays recursively

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



array_multisort: Sort multiple or multi-dimensional arrays

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



array_pad: Pad array to the specified length with a value

<?php
   $states = array("A","B");
   print "before pad:<br />";
   print_r($states);
   $states = array_pad($states,4,"C");
   $states = array("A","B","C","D");
   print "<br />after pad:<br />";
   print_r($states);
?>



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

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



array_reverse: Return an array with elements in reverse order

<?php
   $states = array("A","B","C");
   print "Before reverse: <br />";
   print_r($states);
   print "<br />After reverse: <br />";
   print_r(array_reverse($states));
?>



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

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



Array shuffle

<?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 "<BR><BR><BR>";   
   shuffle($cards);
   // Use array_chunk() to divide the cards into four equal "hands"
   $hands = array_chunk($cards, 4);
   print_r($hands);
?>



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

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



array_sum: Calculate the sum of values in an array

<?php
   $grades = array(42,"hello",42);
   $total = array_sum($grades);
   print $total;
?>



array_unshift

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



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

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



current: Return the current element in an array

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



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

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



in_array: Checks if a value exists in an array

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



next: Advance the internal array pointer of an array

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



The count() Function

<?php
  $employee []= "A";
  $employee []= "B";
  $employee []= "C";
  $employee []= "D";
  echo count ($employee);
?>



Using the array_diff () Function

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



Using the array_intersect () Function

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



Using the array_merge () Function

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



Using the array_push () Function

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



Using_the array_shift () Function

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



Using the array_slice () Function

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



Using the array_unique () Function

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