سؤال

I have a small task in 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. How can i do it in shortest php script?

هل كانت مفيدة؟

المحلول

With array_slice, for example:

$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?)

نصائح أخرى

use array_slice for that :D

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

use array_slice.

$count =3;
$slice_array=array_slice($levels,0,$count);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top