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

时间:2014-12-03 16:01:18

标签: php multidimensional-array

我有一个对象数组,每个对象都有一个唯一的随机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的原始数组键?

2 个答案:

答案 0 :(得分:26)

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

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

答案 1 :(得分:6)

似乎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)
  }
}