PHP commonly used arrays Array Function Example Summary "assignment, split, merge, calculate, add, delete, query, judge, sort" _php tips

Source: Internet
Author: User
Tags exception handling php error rand shuffle

The example of this article summarizes the common PHP array function. Share to everyone for your reference, specific as follows:

Array_combine

Function: Use the value of one array as the key name of the new array, and the value of the other array as the value of the new array

Case:

<?php
$a = Array ("One", "two", "three");
$b = Array ("One", "two", "three");
$c = Array_combine ($a, $b);
Print_r ($c);
/** results
*array ([one] => [two] => two [three] => III)
* *

Array_chunk

Function: Split fractions to form multiple arrays

<?php
$input _array = Array ("A" => "Apple", "B" => "Blue", "C", "D", "E");
echo "<pre>";
Print_r (Array_chunk ($input _array, 2));
Print_r (Array_chunk ($input _array, 2,true));
echo "</pre>";
/** result
Array
(
 [0] => array
  (
   [0] => Apple
   [1] => blue
  )
 [1] => array< c15/> (
   [0] => C
   [1] => D
  )
 [2] => Array
  (
   [0] => e
  )
)
array< c25/> (
 [0] => array
  (
   [a] => Apple
   [b] => blue
  )
 [1] => array
  (
   [0 ] => c
   [1] => D
  )
 [2] => Array
  (
   [2] => e
  )
)
* *

Array_count_values

Function: Count the number of occurrences of a value in an array

<?php
$input _array = Array ("A" => "Apple", "B" => "Blue", "C", "D", "E");
echo "<pre>";
Print_r (Array_count_values ($input _array));
echo "</pre>";
/** result
Array
(
 [Apple] => 1
 [blue] => 1
 [c] => 1
 [d] => 1
 [E] => 1
)
*/

Array_diff

Function: Remove the data from the second array in the first array, and return the remainder as the result

<?php
$array 1 = Array ("A" => "Apple", "B" => "Blue", "C", "D", "E");
$array 2 = Array ("Apple", "C", "D", "F");
$result = Array_diff ($array 1, $array 2);
$result 2 = Array_diff ($array 2, $array 1);
echo "<pre>";
Print_r ($result);//array 1 removes the remaining
Print_r ($result 2) from array 2;//array 2 to remove the remaining
echo "</pre>" in array 1;
/** result
Array
(
 [b] => blue
 [2] => e
)
array
(
 [3] => f
)
* /

Array_map

Function: Executes the callback function into the array

<?php
//defines the callback functions function
cube ($n) {return
 ($n * $n * $n);
}
$a = array (1,2,3,4,5);
$b = Array_map ("cube", $a);
echo "<pre>";
Print_r ($b);
echo "</pre>";
/** result
Array
(
 [0] => 1
 [1] => 8
 [2] =>
 [3] =>
 [4] =>
) c42/>*/

Array_merge

Function: Merge one or more arrays

Note: If the following key name will overwrite the previous content, the key number will be added to the back

<?php
$array 1 = Array ("Color" => "red", 2,4);
$array 2 = Array ("A", "B", "Color" => "green", "shape" => "trapezoid", 4);
$result 1 = array_merge ($array 1, $array 2);
$result 2 = array_merge_recursive ($array 1, $array 2);
echo "<pre>";
Print_r ($result 1);
Print_r ($result 2);
echo "</pre>";
/** result
Array
(
 [color] => green
 [0] => 2
 [1] => 4
 [2] => a
 [3] => B
   [shape] => trapezoid
 [4] => 4
)
array
(
 [color] => array
  (
   [0] => Red
   [1] => green
  )
 [0] => 2
 [1] => 4
 [2] => a
 [3] => b
 [shape] => trapezoid
 [4] => 4
)
* /

Array_pop

Function: Remove the last element of the array and return the element content that was excluded

<?php
$stack = Array ("Orange", "banana", "apple", "1");
$last = Array_pop ($stack);
echo "<pre>";
Print_r ($stack);
Print_r ($last);
echo "</pre>";
/** result
Array
(
 [0] => Orange
 [1] => banana
 [2] => Apple
)
1
*

Array_push

Function: Presses a plurality of cells into the end of the array, returns the number of arrays after

<?php
$stack = Array ("Orange", "banana");
$count = Array_push ($stack, "Apple", "Red", "Blue");
echo "<pre>";
Print_r ($stack);
Print_r ($count);
echo "</pre>";
/** result
Array
(
 [0] => Orange
 [1] => banana
 [2] => Apple
 [3] => red
 [4] =& Gt Blue
)
5
*/

Array_rand

function: Get a random key name

<?php
$input = Array ("Orange", "banana", "apple", "Red", "Blue");
$rand = Array_rand ($input, 2);
Print_r ($rand);
$rand = Array_rand ($input, 3);
Print_r ($rand);
/** result
Array
(
 [0] => 1
 [1] => 4
)
array
(
 [0] => 0
 [1] => 1
 [2] => 3
)
*/

Array_search

Function: Query the contents of the array, return the key value, if there are more than one match, return the first matching content

<?php
$array = Array ("Blue" => "B", "Red" => "R", "Green", "R");
$key = Array_search (' B ', $array);
echo $key;
echo "<br>";
$key = Array_search (' R ', $array);
echo $key;
echo "<br>";
/** results
Blue
Red
* *

Array_shift

Function: Remove the beginning element, contrary to Array_pop

<?php
$fruit = Array ("Milk", "orange", "banana", "apple");
$top = Array_shift ($fruit);
Print_r ($top);
echo "<br>";
Print_r ($fruit);
/** results
Milk
Array ([0] => Orange [1] => banana [2] => apple)
* *

Array_unique

Function: Remove the elements that are duplicated in the array, preserving the first occurrence, including the key name and value

<?php
$input = Array ("A" => "green", "Red", "B" => "green", "Blue", "C" => "Red");
$result = Array_unique ($input);
Print_r ($result);
echo "<br>";
Print_r ($input);
/** result
Array ([a] => green [0] => red [1] => Blue)
Array ([a] => green [0] => Red [b] => GRE En [1] => blue [C] => red)
* *

Array_slice

Function: Remove part of an element from an array

<?php
$input = Array ("A", "B", "C", "D", "E");
$output = Array_slice ($input, 2);//When the second argument is not, the representation is taken to the last element
Print_r ($output);
echo "<br>";
$output = Array_slice ($input, -2,1);//When the second argument is positive, the number is represented; the first is 1, and the second is -2
print_r ($output);
echo "<br>";
$output = Array_slice ($input, 0,3);
Print_r ($output);
echo "<br>";
$output = Array_slice ($input, 2,-1);//The second parameter is a negative number, which is the position, which is taken, not including itself
Print_r ($output);
echo "<br>";
$output = Array_slice ($input, 2,-1,true);//When the third argument is true, the original key value
Print_r ($output) is preserved;
echo "<br>";
/** result
Array ([0] => C [1] => D [2] => e)
Array ([0] => D)
Array ([0] => a [1] => b [ 2] => c)
Array ([0] => C [1] => D)
Array ([2] => C [3] => D)
* *

Count

Function: Returns the number of elements in an array.

<?php
$input = Array ("A", "B", "C", Array ("D", "E"));
$count = count ($input);
echo $count;
echo "<br>";
$input = Array ("A", "B", "C", "D", "E");
$count = count ($input);
echo $count;
/** Results
4
5
* *

Current

Function: Gets the current pointer to the element

<?php
$array = Array ("Foot", "bike", "car", "plane");
$result = current ($array);
echo $result. " <br> ";
Next ($array);//make pointer to next element
$result = current ($array);
echo $result. " <br> ";
Prev ($array);//Make the pointer point to the previous element
$result = current ($array);
echo $result. " <br> ";
End ($array);//Make the pointer point to the last element
$result = current ($array);
echo $result. " <br> ";
/** results
foot
bike
foot
plane
* *

In_array

Function: Verifies that a value exists in an array, returns TRUE, does not return false

<?php
$os _list = Array ("Mac", "NT", "Irix", "Linux");
if (In_array ("Irix", $os _list)) {
 echo "exists Irix in the current operating system list";
} else{
 echo "does not exist in the current operating system list Irix";
}
echo "<br>";
if (In_array ("Mac", $os _list)) {
 echo "Mac" exists in the current operating system list;
} else{
 echo "Mac" does not exist in the current operating system list;
}
echo "<br>";
/** results
The current operating system list exists IRIX the
current operating system does not exist in the list of Mac
* *

List

function: Assigning information in an array to multiple variables

<?php
$info = Array ("Red", "Blue", "green");
List ($flag, $sky, $grassland) = $info;
echo "$flag, $sky, $grassland";
echo "<br>";
List ($flag,, $grassland) = $info;
echo "$flag, $grassland";
echo "<br>";
List (,, $grassland) = $info;
echo "$grassland";
echo "<br>";
/** results
red,blue,green
red,green
Green
* *

Shuffle

Features: Scrambling arrays

<?php
$numbers = range (1,5);//Generate a random array
print_r ($numbers);
echo "<br/>";
Shuffle ($numbers);//Disturb array
print_r ($numbers);
/** result
Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5)
Array ([0] => 4 [1] => 1 [2] =& Gt 5 [3] => 2 [4] => 3)
* *

Array_keys

Function: Gets the key name of the array, and the second parameter can specify that an element be fetched

<?php
$array = Array (0=>100, "Color" => "red");
Print_r (Array_keys ($array));
echo "<br>";
$array = Array ("Blue", "Red", "green", "Blue", "Blue");
Print_r (Array_keys ($array, "Blue"));
echo "<br>";
$array = Array ("Color" =>array ("Blue", "Red", "green"), "Size" =>array ("small", "medium", "large"));
Print_r (Array_keys ($array));
echo "<br>";
/** result
Array ([0] => 0 [1] => color)
Array ([0] => 0 [1] => 3 [2] => 4)
Array ([0] => Color [1] => size)
* *

Array_reverse

Function: Gets the inverse of the array

<?php
$input = Array ("PHP", 3.0,array ("Green", "red"));
$result = Array_reverse ($input); Upset key name
$result _keyed = Array_reverse ($input, TRUE);//reserve Key
name Print_r ($result);
Print_r ($result _keyed);
/** result
Array
(
 [0] => array
  (
   [0] => Green
   [1] => red
  )
 [1] => 3
 [2] => php
)
Array
(
 [2] => array
  (
   [0] => Green
   [1] => red
  )
 [1] => 3
 [0] => PHP
)
*/

Arsort

Function: Reverse sort, index unchanged

<?php
$fruits = Array (
 "a" => "lemon",
 "B" => "Orange",
 "C" => "banana",
 "D" => "Apple",
 );
Arsort ($fruits);//reverse-sort by character or digital
foreach ($fruits as $key => $val) {
 echo $key = $val <br>;
}
/** result
B = orange
a = lemon
c = Banana
d = Apple
* *

Asort

Function: Perform forward sort

<?php
$fruits = Array (
 "a" => "lemon",
 "B" => "Orange",
 "C" => "banana",
 "D" => " Apple ",
 );
Arsort ($fruits);//reverse-sort by character or digital
foreach ($fruits as $key => $val) {
 echo $key = $val <br>;
}
echo "<p>";
Asort ($fruits)//by Word single-character to sort or numerically
foreach ($fruits as $key => $val) {
 echo $key = $val <br>;
}
/** result
B = orange
a = lemon
c = Banana
d = Apple
d = apple
c = Banana
a = lemon
B = Orange
* *

Krsort

Function: Reverse sort according to key name

<?php
$fruits = Array (
 "a" => "lemon",
 "B" => "Orange",
 "C" => "banana",
 "D" => "Apple",
 );
Krsort ($fruits);//In reverse order by key name or number
foreach ($fruits as $key => $val) {
 echo "$key = $val <br>";
}
/** result
d = apple
c = Banana
B = orange
a = lemon
* *

Ksort

Function: Forward sort according to key name

<?php
$fruits = Array (
 "a" => "lemon",
 "B" => "Orange",
 "C" => "banana",
 "D" => " Apple ",
 );
Ksort ($fruits)//By the key name forward sort or number
foreach ($fruits as $key => $val) {
 echo $key = $val <br>;
}
/** result
a = lemon
b = orange
c = Banana
d = Apple
* *

Rsort

Function: Reverse sort by value, key name change

<?php
$fruits = Array (
 "a" => "lemon",
 "B" => "Orange",
 "C" => "banana",
 "D" => "Apple",
 );
Rsort ($fruits);//In reverse order or number by value, the key name changes
foreach ($fruits as $key => $val) {
 echo "$key = $val <br>";
}
/** result
0 = orange
1 = lemon
2 = Banana
3 = Apple
* *

Sort

Function: Forward sort by value, key name changed

<?php
$fruits = Array (
 "a" => "lemon",
 "B" => "Orange",
 "C" => "banana",
 "D" => " Apple ",
 );
Sort ($fruits);//In reverse order or number by value, the key name changes
foreach ($fruits as $key => $val) {
 echo "$key = $val <br>";
}
/** result
0 = Apple
1 = banana
2 = lemon
3 = Orange
* *

More interested in PHP related content readers can view the site topics: "PHP array" Operation tips Daquan, "PHP string (String) Usage summary", "PHP common functions and techniques summary", "PHP error and Exception handling method summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented Programming", "PHP+MYSQL Database Operations Introduction Tutorial" and "PHP Common database Operation tips Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.