php 中常用数组操作 数组分割 次数 键名差集
本教程收藏了大量的php初学者要用到的数组实例,包括有创建数组 用前两个数组值作为新数组的键与值 数据返回成字符串 将数组分割,不保留原数组键名 把原数组元素出现次数赋值给新数组并显示 对键名计算差集等数据操作
本教程收藏了大量的php教程初学者要用到的数组实例,包括有创建数组 用前两个数组值作为新数组的键与值 数据返回成字符串 将数组分割,不保留原数组键名 把原数组元素出现次数赋值给新数组并显示 对键名计算差集等数据操作
*/
//
$array=array(1,1,1,1,1,8=>1,4=>1,19,3=>13); //创建数组
print_r($array); //输出数组内容
//
$a=array('green','red','yellow'); //定义第一个数组
$b=array('avocado','apple','banana'); //定义第二个数组
$c=array_combine($a,$b); //用前两个数组值作为新数组的键与值
print_r($c); //输出新建的数组
//
foreach(range(0,12)as $number) //返回数组0-12
{
echo $number.",";
}
echo "
";
foreach(range(0,100,10)as $number) //返回数组0,10,20……100
{
echo $number.",";
}
echo "
";
foreach(range('a','i')as $letter)
{
echo $letter.",";
}
echo "
";
foreach(range('c','a')as $letter) //返回数组c,b,a
{
echo $letter.",";
}
//
$input_array=array('a','b','c','d','e'); //定义初始数组
print_r(array_chunk($input_array,2)); //将数组分割,不保留原数组键名
print_r(array_chunk($input_array,2,true)); //将数组分割,保留原数组键名
//
$array=array(1,"hello",1,"php","hello"); //定义一个数组
print_r(array_count_values($array)); //把原数组元素出现次数赋值给新数组并显示
//
$array1=array("a"=>"green","b"=>"brown","c"=>"blue","red"); //定义数组1
$array2=array("a"=>"green","yellow","red"); //定义数组2
$result=array_diff_assoc($array1,$array2); //把两个数组的差集赋值给数组
print_r($result); //输出差集的内容
//
$array1=array('blue'=>1,'red'=>2,'green'=>3); //定义数组1
$array2=array('green'=>4,'blue'=>5,'yellow'=>6); //定义数组2
$result=var_dump(array_diff_key($array1,$array2)); //对键名计算差集
print_r($result);
//
//定义回调函数
function key_compare_func($a, $b)
{
if($a==$b)
{
return 0; //如果两参数相等,返回0
}
return($a>$b)?1:-1; //如果$a>$b返回1,小于则返回-1
}
//分别定义两个数组
$array1=array("a"=>"green","b"=>"brown","c"=>"blue","red");
$array2=array("a"=>"green","yellow","red");
//用回调函数做索引检查来计算数组的差集
$result=array_diff_uassoc($array1,$array2,"key_compare_func");
print_r($result);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver CS6
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
