php 如何在使用array_chunk时保留原始的唯一数组键?

z18hc3ub  于 2022-12-28  发布在  PHP
关注(0)|答案(3)|浏览(117)

我有一个对象数组,每个对象都有一个唯一的随机ID。

111 => 
    object(stdClass)[452]
      public 'Description' => string 'Description here...' (length=728)       
      public 'Name' => string 'Shirt' (length=18)
      public 'Price' => float 36.56

222 => 
    object(stdClass)[452]
      public 'Description' => string 'Description here...' (length=728)       
      public 'Name' => string 'Pants' (length=18)
      public 'Price' => float 36.56

333 => 
    object(stdClass)[452]
      public 'Description' => string 'Description here...' (length=728)       
      public 'Name' => string 'Dress' (length=18)
      public 'Price' => float 36.56

444 => 
    object(stdClass)[452]
      public 'Description' => string 'Description here...' (length=728)       
      public 'Name' => string 'Dress' (length=18)
      public 'Price' => float 36.56

...

我的目标是将对象的键控数组拆分为2个一组的块,以便进行分页。

0 =>
    111 => 
        object(stdClass)[452]
          public 'Description' => string 'Description here...' (length=728)           
          public 'Name' => string 'Shirt' (length=18)
          public 'Price' => float 36.56

    222 => 
        object(stdClass)[452]
          public 'Description' => string 'Description here...' (length=728)           
          public 'Name' => string 'Pants' (length=18)
          public 'Price' => float 36.56
1 =>
    333 => 
        object(stdClass)[452]
          public 'Description' => string 'Description here...' (length=728)           
          public 'Name' => string 'Dress' (length=18)
          public 'Price' => float 36.56

    444 => 
        object(stdClass)[452]
          public 'Description' => string 'Description here...' (length=728)          
          public 'Name' => string 'Dress' (length=18)
          public 'Price' => float 36.56
...

我的问题是使用array_chunk()将对象数组分成2个一组,我的唯一ID没有被保留。

private function paginate($array)
{
    $chunks = 2;
    $paginatedResults = array_chunk($array, $chunks);

    return $paginatedResults;
}

功能输出:

0 =>
    0 => 
        object(stdClass)[452]
          public 'Description' => string 'Description here...' (length=728)
          public 'Name' => string 'Shirt' (length=18)
          public 'Price' => float 36.56

    1 => 
        object(stdClass)[452]
          public 'Description' => string 'Description here...' (length=728)
          public 'Name' => string 'Pants' (length=18)
          public 'Price' => float 36.56
1 =>
    0 => 
        object(stdClass)[452]
          public 'Description' => string 'Description here...' (length=728)
          public 'Name' => string 'Dress' (length=18)
          public 'Price' => float 36.56

    1 => 
        object(stdClass)[452]
          public 'Description' => string 'Description here...' (length=728)
          public 'Name' => string 'Dress' (length=18)
          public 'Price' => float 36.56
...

如何将对象的键控数组拆分为每个索引包含2个对象的另一个数组,同时保留包含唯一ID的原始数组键?

8i9zcol2

8i9zcol21#

我所要做的就是将array_chunk()的第三个参数设置为true,如下所示:

$paginatedResults = array_chunk($array, $chunk, true);
j2datikz

j2datikz2#

看起来array array_chunk ( array $input , int $size [, bool $preserve_keys = false ] )的第三个参数正好控制了这一点。

<?php
$x = array_flip(range('a','j'));
var_dump($x);
var_dump(array_chunk($x, 3, true));

印刷品

array(10) {
  ["a"]=>
  int(0)
  ["b"]=>
  int(1)
  ["c"]=>
  int(2)
  ["d"]=>
  int(3)
  ["e"]=>
  int(4)
  ["f"]=>
  int(5)
  ["g"]=>
  int(6)
  ["h"]=>
  int(7)
  ["i"]=>
  int(8)
  ["j"]=>
  int(9)
}
array(4) {
  [0]=>
  array(3) {
    ["a"]=>
    int(0)
    ["b"]=>
    int(1)
    ["c"]=>
    int(2)
  }
  [1]=>
  array(3) {
    ["d"]=>
    int(3)
    ["e"]=>
    int(4)
    ["f"]=>
    int(5)
  }
  [2]=>
  array(3) {
    ["g"]=>
    int(6)
    ["h"]=>
    int(7)
    ["i"]=>
    int(8)
  }
  [3]=>
  array(1) {
    ["j"]=>
    int(9)
  }
}
vbkedwbf

vbkedwbf3#

如果您想要相同但保留原始数字键:

$array = [0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5];

$chunkSize = 2;
$offset = 0;
$chunks = [];

while ($slice = array_slice($array, $offset, $chunkSize)) {
    $keys = range($offset, $offset + count($slice) - 1);
    $chunks[] = array_combine($keys, $slice);
    $offset += $chunkSize;
}

print_r($chunks);

结果:

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

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

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

在静态Helper函数中:

public static function chunkArrayKeepingOriginalNumericKeys(
        array $array,
        int $chunkSize = 100,
        int $offset = 0
    ): array {
        $chunks = [];

        while ($slice = array_slice($array, $offset, $chunkSize)) {
            $keys = range($offset, $offset + count($slice) - 1);
            $chunks[] = array_combine($keys, $slice);
            $offset += $chunkSize;
        }

        return $chunks;
    }

相关问题