php 我怎样才能取一个数组,将它除以二并创建两个列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5393028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i take an array, divide it by two and create two lists?
提问by Andy
Say i have an array
说我有一个数组
$array
Could anyone give me an example of how to use a foreach loop and print two lists after the initial array total has been counted and divided by two, with any remainder left in the second list?
谁能给我一个例子,说明如何使用 foreach 循环并在对初始数组总数进行计数并除以二后打印两个列表,剩余部分留在第二个列表中?
So instead of just using the foreach to create one long list it will be creating two lists? like so...
因此,不是仅使用 foreach 创建一个长列表,而是创建两个列表?像这样...
- Value 1
- Value 2
- Value 3
- 值 1
- 值 2
- 值 3
and then the second list will continue to print in order
然后第二个列表将继续按顺序打印
- Value 4
- Value 5
- Value 6
- 值 4
- 值 5
- 值 6
回答by Mormegil
To get a part of an array, you can use array_slice:
要获取数组的一部分,您可以使用array_slice:
$input = array("a", "b", "c", "d", "e");
$len = count($input);
$firsthalf = array_slice($input, 0, $len / 2);
$secondhalf = array_slice($input, $len / 2);
回答by meagar
Use array_chunk
to split the array up into multiple sub-arrays, and then loop over each.
使用array_chunk
到阵列向上循环每个分成多个子阵列,然后。
To find out how large the chunks should be to divide the array in half, use ceil(count($array) / 2)
.
要找出将数组一分为二的块应该有多大,请使用ceil(count($array) / 2)
.
<?php
$input_array = array('a', 'b', 'c', 'd', 'e', 'f');
$arrays = array_chunk($input_array, 3);
foreach ($arrays as $array_num => $array) {
echo "Array $array_num:\n";
foreach ($array as $item_num => $item) {
echo " Item $item_num: $item\n";
}
}
Output
输出
Array 0:
Item 0: a
Item 1: b
Item 2: c
Array 1:
Item 0: d
Item 1: e
Item 2: f
回答by Peeter
http://php.net/manual/en/function.array-slice.php
http://php.net/manual/en/function.array-slice.php
To slice the array into half, use floor(count($array)/2) to know your offset.
要将数组切成两半,请使用 floor(count($array)/2) 来了解您的偏移量。
回答by James Tierney
Here's a one-liner which uses array_chunk:
这是一个使用 array_chunk 的单行:
list($part1, $part2) = array_chunk($array, ceil(count($array) / 2));
If you need to preserve keys, add true as the third argument:
如果您需要保留密钥,请添加 true 作为第三个参数:
list($part1, $part2) = array_chunk($array, ceil(count($array) / 2), true);
回答by Shakti Singh
$limit=count($array);
$first_limit=$limit/2;
for($i=0;$i<$first; $i++)
{
echo $array[$i];
}
foreach ($i=$first; $i< $limit; $i++)
{
echo $array[$i];
}
回答by Phenex
Using a foreach loop you could do this:
使用 foreach 循环你可以这样做:
$myarray = array("a", "b", "c", "d", "e", "f", "g");
$array1 = array();
$array2 = array();
$i = 1;
foreach ($myarray as $value) {
if ($i <= count($myarray) / 2) {
$array1[] = $value;
} else {
$array2[] = $value;
}
$i++;
}
But it's even easier to use array_splice
但使用 array_splice 更容易
$myarray = array("a", "b", "c", "d", "e", "f", "g");
$array1 = array_splice($myarray, 0, floor(count($myarray)/2));
$array2 = $myarray;
回答by Nathaniel Meyer
This Worked for me made the first array always a little longer. Thought this might help people too.
这对我有用,使第一个数组总是更长一点。认为这也可能对人们有所帮助。
$firsthalf = array_slice($input, 0, $len / 2 +1);
$secondhalf = array_slice($input, $len / 2 +1);