如何取一个数组,除以2,然后创建两个列表?

时间:2022-09-03 21:44:11

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循环并在初始数组总数被计数并除以2之后打印两个列表的例子吗?

So instead of just using the foreach to create one long list it will be creating two lists? like so...

所以它不是用foreach创建一个长列表而是创建两个列表?像这样…

  1. Value 1
  2. 值1
  3. Value 2
  4. 值2
  5. Value 3
  6. 值3

and then the second list will continue to print in order

然后第二个列表将继续按顺序打印

  1. Value 4
  2. 价值4
  3. Value 5
  4. 值5
  5. Value 6
  6. 价值6

6 个解决方案

#1


66  

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);

#2


15  

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)来了解偏移量。

#3


13  

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

#4


2  

$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];
}

#5


1  

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;

#6


0  

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);

#1


66  

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);

#2


15  

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)来了解偏移量。

#3


13  

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

#4


2  

$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];
}

#5


1  

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;

#6


0  

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);