Blogger Information
Blog 48
fans 0
comment 0
visits 36786
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组的分割、填充、去重、合并等操作
雨天的博客
Original
661 people have browsed it

实例

<?php
//array_slice()从数组中指定位置,返回指定数量的元素
echo '<h3 style="color: #9A0000;">array_slice()从数组中指定位置,返回指定数量的元素</h3>';
var_dump(array_slice($arr1,2));//第二个是起始位置,第三个参数是取的个数
echo '<br>';

//array_unique()去除数组中重复的值
echo '<h3 style="color: #9A0000;">array_unique()去掉数组中重复的值</h3>';
$arr2=[1,22,1,33,8,6,33];
$arr3=['a'=>2,'c'=>2,'k'=>55,'w'=>66];
var_export(array_unique($arr3));
echo '<br>';

//array_fill()填充数组
echo '<h3 style="color: #9A0000;">array_fill() 填充数组</h3>';
var_export(array_fill(3,7,8));//第一个参数是索引值,第二个是长度,第三个是填充值
echo '<br>';

//array_rand()随机数
echo '<h3 style="color: #9A0000;">array_rand() 随机数</h3>';
var_export(array_rand(range(1,10),5));
echo '<br>';

//array_chunk 将大数组分为小数组
echo '<h3 style="color: #9A0000;">array_chunk() 将大数组分为小数组</h3>';
var_export(array_chunk($arr2,2,true));//第三个参数是索引的值不变
echo '<br>';

//array_pad() 将数组用指定的值,填充到指定的长度
echo '<h3 style="color: #9A0000;">array_pad() 将数组用指定的值,填充到指定的长度</h3>';
var_export(array_pad($arr2,12,11));
echo '<br>';

//shuffle() 把数组中的元素随机排序
echo '<h3 style="color: #9A0000;">shuffle() 将数组的元素随机排序</h3>';
$fruits=['苹果','香蕉','橙子','葡萄'];
shuffle($fruits);
var_export($fruits);
echo '<br>';

//array_merge(),合并数组把相同的键名覆盖
echo '<h3 style="color: #9A0000;">array_merge() 合并数组,把相同的键名覆盖</h3>';
$a = ['a'=>'html','b'=>'css','c'=>'java'];
$b = ['a'=>'scss','k'=>'css','q'=>'javascript'];
$s = array_merge($a,$b);
var_export($s);
echo '<br>';

运行实例 »

点击 "运行实例" 按钮查看在线实例


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post