Introduction of Common Array Functions in PHP

  • 2021-07-10 19:01:13
  • OfStack

It is indispensable to look up manuals in programming, so you should learn to use existing things, just like the array processing function in PHP already has a sorting function, so why bother to write bubbles or heap or fast row?

Programming is an indirect process, It is also a process of reuse, To write a good code is indispensable to design patterns to do support, may be for beginners to learn design patterns some difficult (like when I first looked at design patterns, it is really a bit difficult), but when you have a certain amount of code accumulation, when studying design patterns, I feel that design patterns are really useful and can help you write beautiful code. Say a bit of a deviation, or to summarize 1 under the php of the array operation of the common functions it.

The following summary of the array commonly used functions, some readers may feel a little less, and people gather firewood with high flames. If you feel that there are other commonly used array processing functions, leave a comment, don't be stingy with your knowledge, and share things with others is not a very happy thing. Also, the following code comes from my hand, but it was written two years ago. Welcome to criticize and correct me.

array_splice () Deletes the specified element from the array

array_splice (array name, number of deletions from front to back, size of new1 array); If there is no third parameter, there is no return array. If there is no third parameter, the meaning of the second parameter is to keep several from beginning to end

exp:


<?php
  $my_array=array(  // Create an array 
    "hehe"=>"haha",
    "A"=>"lu",
    "lu"=>"ge"
  );
  $new=array_splice($my_array,1,3);  // Use array_splice( Array name, number of deletions from front to back, new1 The size of an array );
  var_dump($new);
?>

Results: array (2) {["A"] = > string(2) "lu" ["lu"]= > string(2) "ge" }

2. Traversal of foreach () Array

Usage: foreach (Array as key name = > Key value) or foreach (array as key value)

exp:


<?php
  $my_array=array(  // Create an array 
    "hehe"=>"haha",
    "A"=>"lu",
    "lu"=>"ge"
  );
  foreach($my_array as $key=>$value)
  {
    echo $key."=>".$value."<br/>";
  }
?>

Output:


hehe=>haha

A=>lu

lu=>ge

3. Sorting of arrays

(1) The key values of sort () and rsort () sort sort () from small to large and rsort () from large to small

sort () exp :


<?php
  $my_array=array(1,2,3,6,7,8,9,4,5);// Create an array 
  sort($my_array);
  foreach($my_array as $keys=>$value)
  {
    echo $keys."=>".$value."<br/>";
  }
?>

Output:


  0=>1

  1=>2
  2=>3
  3=>4
  4=>5
  5=>6
  6=>7
  7=>8
  8=>9

rsort() exp:


<?php
  $my_array=array(1,2,3,6,7,8,9,4,5);// Create an array 
  rsort($my_array);
  foreach($my_array as $keys=>$value)
  {
    echo $keys."=>".$value."<br/>";
  }
?>

Output:


  0=>9
  1=>8
  2=>7
  3=>6  
  4=>5
  5=>4
  6=>3
  7=>2
  8=>1

(2). asort () and arsort () are similar to the above principle 1, but do not change the corresponding relationship between key names and key values

exp:


<?php
  $my_array=array(1,2,3,6,7,8,9,4,5);// Create an array 
  asort($my_array);
  foreach($my_array as $keys=>$value)
  {
    echo $keys."=>".$value."<br/>";
  }
?>

Output:


0=>1
1=>2
2=>3
7=>4
8=>5
3=>6
4=>7
5=>8
6=>9

(3) ksort () and krsort () are sizing sorts of key names

4. Mathematical class functions of arrays

array_sum () calculates all key values of the array and count () calculates the number of elements

exp:


<?php
  $my_array=array(1,2,3,6,7,8,9,4,5);// Create an array 
  echo array_sum($my_array);
?>

Output: 45

5. Other functions

array_unique () strips the same element from the array

in_array () detects whether a value is in an array (returns true and false)

array_search () returns a key or value, and returns the key name corresponding to the key value

shuffle () scrambles the original array


<?php
  $my_array=array(  // Create an array 
    "hehe"=>"haha",
    "A"=>"lu",
    "lu"=>"ge"
  );
  foreach($my_array as $key=>$value)
  {
    echo $key."=>".$value."<br/>";
  }
?>
0

Output:


<?php
  $my_array=array(  // Create an array 
    "hehe"=>"haha",
    "A"=>"lu",
    "lu"=>"ge"
  );
  foreach($my_array as $key=>$value)
  {
    echo $key."=>".$value."<br/>";
  }
?>
1


Related articles: