Home > Backend Development > PHP Tutorial > Share six easy-to-use PHP array Array functions_php examples

Share six easy-to-use PHP array Array functions_php examples

无忌哥哥
Release: 2023-04-03 06:56:02
Original
1579 people have browsed it

这篇文章给大家分享六个比较好用的php数组Array函数,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧

1. array_column 返回输入数组中某个单一列的值。
2. array_filter 用回调函数过滤数组中的元素。
3. array_map 将用户自定义函数作用到给定数组的每个值上,返回新的值。
4. array_walk_recursive 对数组中的每个成员递归地应用用户函数。
5. extract (从数组中将变量导入到当前的符号表),  compact (创建一个包含变量名和它们的值的数组)
6. uasort 使用用户自定义的比较函数对数组中的键值进行排序。

1. array_column 返回输入数组中某个单一列的值。

类似功能有

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

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


// 可能从数据库中返回数组
$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);
Copy after login


输出结果:

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

2. array_filter 用回调函数过滤数组中的元素。


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


输出:

Array
(
    [2] => 2
    [3] => 3
    [4] => 4
)
Copy after login

3. array_map 将用户自定义函数作用到给定数组的每个值上,返回新的值。

此函数有点类似与 array_walk_recursive , 只是多写了一步递归


 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));
Copy after login


输出:

Array(    
[0] => 1    
[1] => 4    
[2] => 9    
[3] => 16    
[4] => 25    
[6] => Array([0] => 4 [1] => 9)
)
Copy after login
 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));
Copy after login


输出:

Array(    
[0] => different    
[1] => same    
[2] => different
)
Copy after login

4. array_walk_recursive 对数组中的每个成员递归地应用用户函数。

此函数有点类似与 array_map, 只是省去了写递归


 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);
Copy after login


输出:

Array(    
[a] => red    
[b] => green    
[c] => blue    
[d] => Array([x] => green [y] => yyy)
)
Copy after login

5. extract (从数组中将变量导入到当前的符号表), compact (创建一个包含变量名和它们的值的数组)


 $a = "Original";
$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
 extract($my_array);
 echo "\$a = $a; \$b = $b; \$c = $c";
Copy after login


输出:

$a = Cat; $b = Dog; $c = Horse
Copy after login
 $firstname = "Peter";
 $lastname = "Griffin";
 $age = "41";
$result = compact("firstname", "lastname", "age");
 print_r($result);
Copy after login


输出:

Array
(
    [firstname] => Peter
    [lastname] => Griffin
    [age] => 41
)
Copy after login

6. uasort 使用用户自定义的比较函数对数组中的键值进行排序(可以对二维数组排序)。


$arr2 = [
  [
    'id' => 3,
    'age' => 33,
  ],
  [
    'id' => 2,
    'age' => 44,
  ],
  [
    'id' => 1,
    'age' => 22,
  ],
];
//按age字段升序排序
uasort($arr2, function($a, $b) {
  $field = 'age';
  if ($a[$field] == $b[$field]){
    return 0;
  }
  return ($a[$field] < $b[$field]) ? -1 : 1;
});
print_r($arr2);
Copy after login


输出:

Array(    
        [2] => Array([id] => 1 [age] => 22  )
    [0] => Array([id] => 3 [age] => 33   )
    [1] => Array([id] => 2  [age] => 44   )
)
Copy after login


The above is the detailed content of Share six easy-to-use PHP array Array functions_php examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template