[英]How to remove an Array Key From an Array_Chunck?
I have array_chunk
a set of 10 keys within each array that loops through. 我有
array_chunk
在每个数组中循环通过的一组10个键。 I want to remove the key [10]=> from each array that is looped so I will have 9 keys remaining. 我想从循环的每个数组中删除键[10] =>,这样我将剩下9个键。 What I have so far.
到目前为止我有什么。
$page = array( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s");
$number_of_pages_to_copy = 5;
$x = 1;
while($x <= $number_of_pages_to_copy) {
$sum = array_chunk($page, 11);
unset($sum['10']);
print_r($sum);
$x++;
}
Current output: 当前输出:
Array ( [0] => Array ( [1] => "a" [2] => "b" [3] => "c" [4] => "d" [5] => "e" [6] => "f" [7] => "g" [8] => "h" [9] => "i" [10]=> "j" )
[1]=> Array ( [1] => "a" [2] => "b" [3] => "c" [4] => "d" [5] => "e" [6] => "f" [7] => "g" [8] => "h" [9] => "i" [10]=> "j" )
Desired output: 期望的输出:
Array ( [0] => Array ( [1] => "a" [2] => "b" [3] => "c" [4] => "d" [5] => "e" [6] => "f" [7] => "g" [8] => "h" [9] => "i" )
[1] => Array ( [0] => Array ( [1] => "a" [2] => "b" [3] => "c" [4] => "d" [5] => "e" [6] => "f" [7] => "g" [8] => "h" [9] => "i" )
Nothing is happening, The unset seems not to be working. 没有任何事情发生,未设置似乎没有工作。
Your $sum is an array of array, so you have to use another index or foreach. 你的$ sum是一个数组数组,所以你必须使用另一个索引或foreach。
array_slice($sum[0],0,10);
or 要么
foreach($sum as &$v)
{
unset($v[10]);
}
for you unset the last index of the array, so you neednot to reindex the array, if not use array_values after unset. 为你取消设置数组的最后一个索引,所以你不需要重新索引数组,如果不设置则不使用array_values。
Here is a demo: 这是一个演示:
<?php
$page = array( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s");
$arrays= array_chunk($page, 11);
foreach($arrays as &$array)
{
unset($array[10]);
}
print_r($arrays);
output: 输出:
ei@localhost:~$ php test.php
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
[6] => g
[7] => h
[8] => i
[9] => j
)
[1] => Array
(
[0] => l
[1] => m
[2] => n
[3] => o
[4] => p
[5] => q
[6] => r
[7] => s
)
)
or 要么
<?php
$page = array( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s");
$arrays= array_chunk($page, 11);
$arrays = array_map(function($v){return array_slice($v, 0, 10);}, $arrays);
print_r($arrays);
and this will get the same output. 这将获得相同的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.