需要帮助拆分数组

时间:2015-02-19 08:47:00

标签: php arrays laravel

您好我正在尝试将数组拆分为2个或更多数组。这是我想要做的:

我的阵列:

array(size=5)
  0=>
    array(size=3)
      'oid' => int 1
      payment_status => 'Paid'
      payment_type => 'Online'
  1=>
    array(size=3)
      'oid' => int 1
      payment_status => 'Paid'
      payment_type => 'Online'
  2=>
    array(size=3)
      'oid' => int 1
      payment_status => 'Paid'
      payment_type => 'Online'
  3=>
    array(size=3)
      'oid' => int 2
      payment_status => 'Paid'
      payment_type => 'Online'
  4=>
    array(size=3)
      'oid' => int 2
      payment_status => 'Paid'
      payment_type => 'Online'

我试图像这样拆分这个数组:

array(size=3)
  0=>
    array(size=3)
      'oid' => int 1
      payment_status => 'Paid'
      payment_type => 'Online'
  1=>
    array(size=3)
      'oid' => int 1
      payment_status => 'Paid'
      payment_type => 'Online'
  2=>
    array(size=3)
      'oid' => int 1
      payment_status => 'Paid'
      payment_type => 'Online'

array(size=2)
  0=>
    array(size=3)
      'oid' => int 2
      payment_status => 'Paid'
      payment_type => 'Online'
  1=>
    array(size=3)
      'oid' => int 2
      payment_status => 'Paid'
      payment_type => 'Online'

因此,您可以看到数组基于' oid'被分成多个数组。值。谁能告诉我如何实现这个输出?谢谢

1 个答案:

答案 0 :(得分:2)

简单的,将基于oid的项目复制到一个新数组中:

$input = $yourArray;

$output = array();

foreach($input as $item)
{
    $output[$item['oid']][] = $item;
}

var_dump($output);

输出包含一个以oid为键的数组。

您可以像这样访问分离的数组:

$output[1]; //for oid 1
$output[2]; //for oid 2

等等