我有一个有趣的问题。我有一个像这样的多维数组:
Array
(
[0] => Array
(
[0] => 4
[1] => 4
[2] => 4
[3] => 4
[4] => 3
[5] => 3
[6] => 3
[7] => 2
)
[1] => Array
(
[0] => 4
[1] => 4
[2] => 3
[3] => 3
[4] => 2
)
)
我希望能够使用array_slice从每个子数组中删除一定比例的行。
根据此处的上一篇文章(array_slice in multidimensional array?),我可以轻松地删除固定数量的行。但是说我想删除每个子数组的70%,该子数组具有不同数量的行,我有点卡住了。
答案 0 :(得分:1)
类似....
<?php
$items = array (
array ( 1, 7, 23, 32, 64, 77 ),
array ( 23, 32, 64, 77 ),
array ( 1, 5, 7, 13, 16, 23, 32, 64, 77 )
);
// (?), remove, or keep
$type = 'remove';
// percentage (%)
$percent = 60;
$format = $type == 'remove' ? ( 100 - $percent ) : $percent;
foreach ( $items AS $array => $values )
{
$max = round ( ( ( count ( $values ) * $format ) / 100 ) );
$items[$array] = array_slice ( $values, 0, $max );
}
print_r ( $items );
?>