Share six more useful PHP arrays Array function _php instance

Source: Internet
Author: User
Tags compact
This article for you to share six more useful PHP array function, very good, with a certain reference value, need to refer to a friend

1. Array_column returns the value of a single column in the input array.
2. Array_filter filters the elements in the array with a callback function.
3. Array_map the user-defined function to each value on the given array, returning the new value.
4. array_walk_recursive applies the user function recursively to each member in the array.
5. Extract (import variables from an array into the current symbol table), compact (create an array containing the variable names and their values)
6. Uasort uses a user-defined comparison function to sort the key values in the array.

1. Array_column returns the value of a single column in the input array.

Similar features are

1.1 Yii2 in the Arrayhelper::index ($array, ' id ');

1.2 Yii2 in the $query->select ([' last_name ', ' id '])->indexby (' id ')->column ();


It is possible to return the array from the database $ A = Array (  ' id ' = ' 5698,  ' first_name ' = ' Peter ', ' last_name ' = '  Griffin ',), Array (  ' id ' = = 4767,  ' first_name ' = ' Ben ',  ' last_name ' = ' Smith ',), array (  ' id ' = = 3809,< c7/> ' first_name ' = ' Joe ',  ' last_name ' = ' Doe ', '), $last _names = Array_column ($a, ' last_name ', ' id ');p rint_r ($last _names);


Output Result:

Array (    [5698] = Griffin    [4767] = Smith    [3809] = Doe)

2. Array_filter filters the elements in the array with a callback function.


function Test_odd ($var) {  return ($var & 1);} $a 1=array ("a", "B", 2,3,4);p Rint_r (Array_filter ($a 1, "test_odd"));


Output:

Array (    [2] = 2    [3] = 3    [4] = 4)

3. Array_map the user-defined function to each value on the given array, returning the new value.

This function is somewhat similar to array_walk_recursive, just one more step recursion


function MyFunction ($v) {   if (Is_array ($v)) {     return Array_map ("MyFunction", $v);   }  Return ($V * $v);}  $a = Array (1, 2, 3, 4, 5, 6 = [2, 3]);p Rint_r (Array_map ("MyFunction", $a));


Output:

Array (    [0] = 1    [1] = 4    [2] = 9    [3] [+]    [4] =    [6] = = Array ([0] =&G T 4 [1] = 9))
function MyFunction ($v 1, $v 2) {  if ($v 1 = = = $v 2) {     return "Same";   }   return "Different"; }  $a 1 = array ("Horse", "Dog", "Cat"), $a 2 = Array ("Cow", "Dog", "Rat"), Print_r (Array_map ("MyFunction", $a 1, $a 2));


Output:

Array (    [0] = different    [1] = same    [2] = different)

4. array_walk_recursive applies the user function recursively to each member in the array.

This function is somewhat similar to the Array_map, except that the write recursion is omitted


Function MyFunction (& $value, $key, $p) {   if ($value = = ' xxx ') {     $value = $p;   }} $a = Array ("a" and "=" Red "," b "=" green "," C "and" Blue ", ' d ' = = [' x ' = ' + ' xxx ', ' y ' = ' yyy ']); Array_walk_recursive ($a, "myfunction", ' green '); Print_r ($a);


Output:

Array (    [a] = Red    [b] = green    [c] = = Blue    [d] = = Array ([x] = green [y] = = yyy))

5. Extract (import variables from an array into the current symbol table), compact (create an array containing the variable names and their values)


$a = "Original"; $my _array = Array ("A" = "Cat", "b" = "Dog", "c" = "Horse"); Extract ($my _array); echo "\ $a = $a; \ $b = $b; \ $c = $c ";


Output:

$a = Cat; $b = Dog; $c = Horse
$firstname = "Peter"; $lastname = "Griffin"; $age = "a", $result = Compact ("FirstName", "LastName", "age"); Print_r ($result);


Output:

Array (    [FirstName] = Peter    [LastName] = Griffin    [age] + 41)

6. Uasort uses a user-defined comparison function to sort the key values in the array (two-dimensional arrays can be sorted).


$arr 2 = [  [    ' id ' = = 3, ' age ' = +,  ],  [    ' id ' = 2, ' age ' = ',    '  ],  [    ' id ' = 1,    ' age ', '],];//' by the age  word orderby order Uasort ($arr 2, function ($a, $b) {  $field = ' age '; C18/>if ($a [$field] = = $b [$field]) {    return 0;  }  return ($a [$field] < $b [$field])? -1:1;}); Print_r ($arr 2);


Output:

Array ([            2] = = Array ([id] = 1 [age] = +  )    [0] = = Array ([ID] + 3 [age] =   ) 
  [1] = = Array ([id] = 2  [age] = +)   )


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.