简体   繁体   English

如何从PHP中的特定位置取消设置所有高位数组值

[英]How to unset all upper array values from a specific location in php

I have a small task in php. 我在php中有一个小任务。 I have these two arrays. 我有这两个数组。

Images 图片

Array
(
    [0] => 420157_NpFQlK
    [1] => 420157_5yVlp9
    [2] => 420157_hqioBU
    [3] => 420157_a5wbZB
)

Levels 等级

Array
(
    [0] => 50
    [1] => 100
    [2] => 150
    [3] => 200
    [4] => 250
    [5] => 300
    [6] => 350
    [7] => 400
)

Now i need to display result like this 现在我需要显示这样的结果

Images 图片

Array
(
    [0] => 420157_NpFQlK
    [1] => 420157_5yVlp9
    [2] => 420157_hqioBU
    [3] => 420157_a5wbZB
)

Levels 等级

Array
(
    [0] => 50
    [1] => 100
    [2] => 150

)

i want to unset all the upper levels. 我想取消所有上级设置。 I mean if count of images array is 4 the unset levels[3](4th index) and all upper levels. 我的意思是,如果图像数组的数量为4,则未设置级别[3](第4个索引)和所有更高级别。 How can i do it in shortest php script? 如何在最短的php脚本中执行此操作?

With array_slice , for example: 使用array_slice ,例如:

$levels = array_slice($levels, 0, count($images) - 1);

You should pay some attention to the arguments so that this works exactly how you want it to in edge cases as well (for example what happens if $images is empty?) 您应该注意这些参数,以便它在边缘情况下也能按您希望的方式正确运行(例如,如果$images为空会发生什么?)

为此使用array_slice:D

$subArray = array_slice($arr,0,$index);
<?php array_slice($levels, 0,count($images)-1); ?>

use array_slice. 使用array_slice。

$count =3;
$slice_array=array_slice($levels,0,$count);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM