Share six easy to use php array Array functions

  • 2021-10-25 06:05:41
  • OfStack

1. array_column returns the value of a single 1 column in the input array.
2. array_filter filters elements in an array with a callback function.
3. array_map applies a user-defined function to each value of a given array, returning a new value.
4. array_walk_recursive recursively applies the user function to each member of the array.
5. extract (imports variables from an array into the current symbol table), compact (creates an array containing variable names and their values)
6. uasort uses a user-defined comparison function to sort the key values in an array.

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

Similar functions include

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

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


//  Possible return of arrays from a database 
$a = array(
 array(
  'id' => 5698,
  'first_name' => 'Peter',
  'last_name' => 'Griffin',
 ),
 array(
  'id' => 4767,
  'first_name' => 'Ben',
  'last_name' => 'Smith',
 ),
 array(
  'id' => 3809,
  'first_name' => 'Joe',
  'last_name' => 'Doe',
 )
);
$last_names = array_column($a, 'last_name', 'id');
print_r($last_names);

Output:

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

2. array_filter filters elements in an array with a callback function.


function test_odd($var)
{
  return($var & 1);
}
$a1=array("a","b",2,3,4);
print_r(array_filter($a1,"test_odd"));

Output:

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

3. array_map applies a user-defined function to each value of a given array, returning a new value.

This function is somewhat similar to array_walk_recursive, except that one more recursion step is written


 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]);
print_r(array_map("myfunction", $a));

Output:

Array
(
[0] = > 1
[1] = > 4
[2] = > 9
[3] = > 16
[4] = > 25
[6] = > Array
(
[0] = > 4
[1] = > 9
)

)


 function myfunction($v1, $v2) {
  if ($v1 === $v2) {
     return "same";
   }
   return "different";
 } 
 $a1 = array("Horse", "Dog", "Cat");
 $a2 = array("Cow", "Dog", "Rat");
 print_r(array_map("myfunction", $a1, $a2));

Output:

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

4. array_walk_recursive Recursively applies the user function to each member of the array.

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


 function myfunction(&$value, $key, $p) {
   if ($value == 'xxx') {
     $value = $p;
   }
 }
 $a = array("a" => "red", "b" => "green", "c" => "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 (imports variables from an array into the current symbol table), compact (creates an array containing 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 = "41";
$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 an array (you can sort 2-dimensional arrays).


$arr2 = [
  [
    'id' => 3,
    'age' => 33,
  ],
  [
    'id' => 2,
    'age' => 44,
  ],
  [
    'id' => 1,
    'age' => 22,
  ],
];
// Press age Ascending sort of fields 
uasort($arr2, function($a, $b) {
  $field = 'age';
  if ($a[$field] == $b[$field]){
    return 0;
  }
  return ($a[$field] < $b[$field]) ? -1 : 1;
});
print_r($arr2);

Output:

Array
(
[2] = > Array
(
[id] = > 1
[age] = > 22
)

[0] = > Array
(
[id] = > 3
[age] = > 33
)

[1] = > Array
(
[id] = > 2
[age] = > 44
)

)

Summarize


Related articles: