PHP中的数组函数

array_change_key_case
改变数组键值大小写
$array=['NIHAO'=>1,'hello'=>2];
$case_array = array_change_key_case($array)
得:$case_array = ['nihao'=>1,'hello'=>2];

大写:
$case_array = array_change_key_case($array,CASE_UPPER);
得:$case_array = ['NIHAO'=>1,"HELLO"=>2];

array_chunk
分割数组将一维分为二维
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
print_r(array_chunk($cars,2));
得到:
Array ( 
[0] => Array ( [0] => Volvo [1] => BMW ) 
[1] => Array ( [0] => Toyota [1] => Honda ) 
[2] => Array ( [0] => Mercedes [1] => Opel )
)
array_column
返回某一列的所有值
$a = array(
  array(
    'id' => 5698,
    'first_name' => 'Bill',
    'last_name' => 'Gates',
  ),
  array(
    'id' => 4767,
    'first_name' => 'Steve',
    'last_name' => 'Jobs',
  ),
  array(
    'id' => 3809,
    'first_name' => 'Mark',
    'last_name' => 'Zuckerberg',
  )
);

$last_names = array_column($a, 'last_name');
print_r($last_names);

得到:
Array
(
  [0] => Gates
  [1] => Jobs
  [2] => Zuckerberg
)
array_key_exists
检测键值是否存在
$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (array_key_exists("Volvo",$a))
  {
  echo "键存在!";
  }
else
  {
  echo "键不存在!";
  }
  
  得:键存在
array_keys
返回数组中所有键名
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
print_r(array_keys($a));
得:
Array ( [0] => Volvo [1] => BMW [2] => Toyota );
array_merge
把多个数组合并成一个数组
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
得:
Array ( [0] => red [1] => green [2] => blue [3] => yellow );
array_merge_recursive
递归合并数组
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge_recursive($a1,$a2));

得:Array ( [a] => red [b] => Array ( [0] => green [1] => yellow ) [c] => blue );
array_search
搜索指定值,并返回相应的键名
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);

得a
array_sum
返回数组中和的值
$a=array(5,15,25);
echo array_sum($a);

得:45

你可能感兴趣的:(PHP中的数组函数)