Home > php教程 > php手册 > php数组求和 数组交集 内容替换等操作

php数组求和 数组交集 内容替换等操作

WBOY
Release: 2016-06-13 10:00:10
Original
1184 people have browsed it

本教程主要讲一下关于数据中级操作,求数组元素和与交集 内容替换 、查找数组内容。

本教程主要讲一下关于数据中级操作,求数组元素和与交集  内容替换 、查找数组内容。
*/
$input=array("red","green","blue","yellow");     //定义原始数组
array_splice($input,2);         //执行去除操作
print_r($input);           //输出处理后结果
$input=array("red","green","blue","yellow");     //定义原始数组
array_splice($input,1,-1);         //执行去除操作并指定长度
print_r($input);           //输出处理后结果
$input=array("red","green","blue","yellow");     //定义原始数组
array_splice($input,1,count($input),"orange");     //执行去除操作,并指定长度及内容
print_r($input);           //输出处理后结果
$input=array("red","green","blue","yellow");     //定义原始数组
array_splice($input,-1,1,array("black","maroon"));    //替换内容为数组的情况
print_r($input);           //输出处理后结果
$input=array("red","green","blue","yellow");     //定义原始数组
array_splice($input,3,0,"purple");
print_r($input);           //输出处理后结果

//
$sweet=array('a'=>'apple','b'=>'banana');     //定义原始数组
$fruits=array('sweet'=>$sweet,'sour'=>'lemon');    //定义另一个数组
function test_print($item,$key)       //用户自定义函数
{
  echo "$key holds $itemn";       //输出两个参数
}
array_walk_recursive($fruits,'test_print');     //对数组成员递归调用自定义函数

//求和
$a=array(2,4,6,8);        //定义原始数组
echo "sum(a)=".array_sum($a)."n";     //求和
$b=array("a"=>1.2,"b"=>2.3,"c"=>3.4);    //定义原始数组
echo "sum(b)=".array_sum($b)."n";     //求和

//数组交集
function strcasecmp($key1, $key2)
{
  if($key1==$key2)         //如果两个参数相等
  return 0;          //返回0
  else if($key1>$key2)        //如果前一个大于后一个
  return 1;          //返回1
  else           //如果前一个小于后一个
  return -1;          //返回-1
}
$a1=array("a"=>"green","b"=>"brown","c"=>"blue","red");    //定义数组1
$a2=array("a"=>"green","b"=>"brown","yellow","red");    //定义数组2
print_r(array_uintersect_uassoc($a1,$a2,"strcasecmp","function"));  //求两数组交集

//数组交集
$a=array("a"=>"green","b"=>"brown","c"=>"blue","red");   //定义数组1
$b=array("a"=>"green","b"=>"brown","yellow","red");   //定义数组2
$result=array_uintersect($a,$b,"strcasecmp");     //计算数组交集
print_r($result);

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template