Blogger Information
Blog 61
fans 0
comment 0
visits 62246
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP中的for(),while(),foreach()循环与array_splice()
Pengsir
Original
1335 people have browsed it

实例

<?php
header("content-type:text/html;charset=utf8");
        echo '<p>日期选择器</p>';
        //for()循环实现年
        $years=range(1990,2018);
        echo '<select name="years">';
        for ($y=0;$y<count($years);$y++){
            echo '<option value='.current($years).'>'.sprintf("%02d",current($years)).'年</option>';
            next($years);
        }
    echo '</select>';
    //while() 循环实现月
    $months=range(1,12);
    echo '<select name="months">';
    $m=0;
    while ($m<count($months)){
        echo '<option value='.current($months).'>'.sprintf('%02d',current($months)).'月</option>';
        next($months);
        $m++;
    }
    echo '</select>';
//    foreach ()对数组遍历实现日
$days=range(1,30);
echo '<select name="days">';
foreach ($days as $value){
    echo '<option value='.$value.'>'.sprintf("%02d",$value).'日</option>';
}
echo '</select>';
echo '<br>';
echo "<pre>";
//array_splice(&input:array,offset:int,[length:int|null=],[replacement:mixed|null=null])   删除或替换部分元素
//正数
$arrs = [1,2,3,4,5,6];
array_splice($arrs,2);
print_r($arrs);
//output: Array ( [0] => 1 [1] => 2 )

//$offset 偏移量 若为负,则从数组末尾倒数该offset开始移除
$arrs = [1,2,3,4,5,6];
array_splice($arrs,-2);
print_r($arrs);
//output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

/*
 * 第三个参数 $length 可省,表示了要移除的长度

省略,移除offset后所有单元
正数,移除offset后指定长度
负数,保留从末尾向前偏移length的单元,移除其他的单元
0,不会移除单元
 */
//正数
$arrs = [1,2,3,4,5,6];
array_splice($arrs,2,3);
print_r($arrs);
//output:Array ( [0] => 1 [1] => 2 [2] => 6 )

//负数
$arrs = [1,2,3,4,5,6];
array_splice($arrs,2,-3);
print_r($arrs);

//output:Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 5 [4] => 6 )

/*
 * 第四个参数 $replacement 传入一个数组 可替换被移除的单元;
 * 如果offset和length并没有产生单元移除,则插入到offset指定的位置
 */
$arrs = [1,2,3,4,5,6];
array_splice($arrs,2,-3,3);
print_r($arrs);
//output:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )

//没有产生偏移的情况 $length = $offset - count($arrs)
$arrs = [1,2,3,4,5,6];
array_splice($arrs,2,-4,3);
print_r($arrs);
//output:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 3 [4] => 4 [5] => 5 [6] => 6 )

运行实例 »

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

总结:

array_slice(array,offset,length,replacement)

array:规定的数组

offset:

1.偏移量。规定取出元素的开始位置。 0为第一个元素。

2.如果该值设置为正数,则从前往后开始取。

3.如果该值设置为负数,则从后向前取 start 绝对值。 -1从数组的倒数第一个元素开始

length:

1.被返回数组的长度(不填默认为原数组长度)。

2.如果该值设置为整数,则返回该数量的元素。

3.如果该值设置为负数,则函数将在数组你设置这个数终止取出(例如-3就是从数组尾端开始从后向前数到-3这个数终止了)。

4.如果该值未设置,则返回从 start 参数设置的位置开始直到数组末端的所有元素。

replacement:

替换元素

运行效果图:

日期选择器.png

手抄:

IMG_2591.JPG

IMG_2590.JPG

Correction status:qualified

Teacher's comments:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!