如何将数组分割成两个php的倍数

时间:2022-11-12 12:38:35

I'm trying to divide an array into a multiple-dimensional array with 2 'elements' in each contained array.

我试图将一个数组划分为一个包含两个元素的多维数组。

So far I've only been able to divide them into a specified number of chunks, but as the number of elements are determined from a foreach loop and call to the database, I'm struggling to divide them into multiples of 2.

到目前为止,我只能将它们划分为指定数量的块,但是由于元素的数量是由foreach循环决定的,并且调用数据库,我很难将它们分割成2的倍数。

foreach ($_POST as $key)
{
    $data[] = $key;
}

echo '<pre>';
  print_r(partition($data, $i));
echo '</pre>';



function partition(Array $list, $p) 
{
  $listlen = count($list);
  $partlen = floor($listlen / $p);
  $partrem = $listlen % $p;
  $partition = array();
  $mark = 0;

  for($px = 0; $px < $p; $px ++)
  {
    $incr = ($px < $partrem) ? $partlen + 1 : $partlen;
    $partition[$px] = array_slice($list, $mark, $incr);
    $mark += $incr;
  }

  return $partition;
}

The desired output would be like this ...

期望的输出是这样的……

Array
(
  [0] => Array
  (
     [0] => img.jpg
     [1] => http://google.com
  )
  [1] => Array
  (
     [0] => img.jpg
     [1] => http://google.com
  )
  [2] => Array
  (
     [0] => img.jpg
     [1] => http://google.com
  )
)

Any help would be appreciated.

如有任何帮助,我们将不胜感激。

Thanks

谢谢

3 个解决方案

#1


3  

Just use the array_chunk function (-;

只需使用array_chunk函数(-;

Example:

例子:

<?php
print_r(array_chunk($data, 2));

#2


1  

Try array_chunk

尝试array_chunk

see below solution:

看到以下的解决方案:

foreach (range(1, 10) as $key)
{
    $data[] = $key;
}
$i = 2;
echo '<pre>';
print_r(partition($data, $i));
echo '</pre>';

function partition(Array $list, $p)
{
    $partition = array_chunk($list, $p, true);

    return $partition;
}

Output:

输出:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
        )

    [1] => Array
        (
            [2] => 3
            [3] => 4
        )

    [2] => Array
        (
            [4] => 5
            [5] => 6
        )

    [3] => Array
        (
            [6] => 7
            [7] => 8
        )

    [4] => Array
        (
            [8] => 9
            [9] => 10
        )

)

#3


0  

array_chunk does this for you.

array_chunk帮你完成。

foreach ($_POST as $key)
{
    $data[] = $key;
}
var_dump(array_chunk($data, 2));

#1


3  

Just use the array_chunk function (-;

只需使用array_chunk函数(-;

Example:

例子:

<?php
print_r(array_chunk($data, 2));

#2


1  

Try array_chunk

尝试array_chunk

see below solution:

看到以下的解决方案:

foreach (range(1, 10) as $key)
{
    $data[] = $key;
}
$i = 2;
echo '<pre>';
print_r(partition($data, $i));
echo '</pre>';

function partition(Array $list, $p)
{
    $partition = array_chunk($list, $p, true);

    return $partition;
}

Output:

输出:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
        )

    [1] => Array
        (
            [2] => 3
            [3] => 4
        )

    [2] => Array
        (
            [4] => 5
            [5] => 6
        )

    [3] => Array
        (
            [6] => 7
            [7] => 8
        )

    [4] => Array
        (
            [8] => 9
            [9] => 10
        )

)

#3


0  

array_chunk does this for you.

array_chunk帮你完成。

foreach ($_POST as $key)
{
    $data[] = $key;
}
var_dump(array_chunk($data, 2));