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

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

Jun 13, 2016 am 10:00 AM
phpone timeandintermediatemainintersectionaboutcontentoperateTutorialdataarrayreplacebegSum

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

本教程主要讲一下关于数据中级操作,求数组元素和与交集  内容替换 、查找数组内容。
*/
$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);

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment