php如何按ID分组

时间:2019-02-21 09:18:23

标签: php arrays multidimensional-array

我是PHP的新手。我正在处理数组数据结构。目前,我得到两个结果,它们是相同的ID,但细节不同。我需要根据ID组合详细信息。

这是我的数组结果

array:2 [
  0 => array:13 [
    "id" => "1"
    "date" => "01-02-2019"
    "order" => array:1 [
      0 => array:2 [
        "code" => "F001"
        "name" => "fish"
      ]
    ]
  ]

  1 => array:13 [
    "id" => "1"
    "date" => "01-02-2019"
    "order" => array:1 [
      0 => array:2 [
        "code" => "F002"
        "name" => "chicken"
      ]
    ]
  ]
]

这是我的预期结果

array:1 [
  0 => array:13 [
    "id" => "1"
    "date" => "01-02-2019"
    "order" => array:2 [
      0 => array:2 [
        "code" => "F001"
        "name" => "fish"
      ]
      1 => array:2 [
        "code" => "F002"
        "name" => "chicken"
      ]
    ]
  ]
]

如果有相同的ID,我正在尝试插入订单数组

这是我的代码

public function order($value)
    {
        $group_id = [];
    $groupItem = [];
    foreach ($value as $item) {
        $item['date'] = date('Y-m-d',strtotime($item['date']));
        $groupItem = $item['order'][] = [
                     'id' => $item['id'],
                     'name' => $item['name'],
                       ];

        $group_id[$item['id']][]= $groupItem; 
    }
}

结果

array:1 [
  "1" => array:1 [
      0 => array:2 [
        "code" => "F001"
        "name" => "fish"
      ]
      1 => array:2 [
        "code" => "F002"
        "name" => "chicken"
      ]
    ]
]

如您所见,我将order分组了,但是日期和ID丢失了。

3 个答案:

答案 0 :(得分:0)

分配适当的数据。试试-

foreach ($value as $item) {
    $date = date('Y-m-d',strtotime($item['date']));
    $id = $item['id'];
    $group_id[$id]['id'] = $id;
    $group_id[$id]['date'] = $date;
    $group_id[$id]['order'][] = [
         'id' => $item['id'],
         'name' => $item['name'],
    ];
}

答案 1 :(得分:0)

如果您需要考虑多个ID,我会这样写:

$result = array_reduce( $input, function ( $result, $item ) {
    $id = $item['id'];

    if ( ! isset( $result[ $id ] ) ) { // We don't have an item yet.
        $result[ $id ] = $item;
    } else { // We have an item and need to add orders to it.
        $result[ $id ]['order'] = array_merge( $result[ $id ]['order'], $item['order'] );
    }

    return $result;
}, [] );

答案 2 :(得分:0)

这是一个工作指令功能:

function order($input) {
    $output = array();
    foreach($input as $item) {
        if (!isset($output[$item['id']])) {
            $output[$item['id']] = array(
                "id" => $item["id"],
                "date" => $item["date"],
                "order" => array()
            );
        }
        $output[$item['id']]['order'][] = $item['order'];
    }
    return array_values($output);
}

输入:

array(
  array(
    "id" => "1",
    "date" => "01-02-2019",
    "order" => array(
      array(
        "code" => "F001",
        "name" => "fish"
      )
    )
  ),

  array(
    "id" => "1",
    "date" => "01-02-2019",
    "order" => array(
      array(
        "code" => "F002",
        "name" => "chicken"
      )
    )
  )
);

输出:

array(
  array(
    "id"=> "1",
    "date"=> "01-02-2019",
    "order"=> array(
      array(
        array(
          "code"=> "F001",
          "name"=> "fish"
        )
      ),
      array(
        array(
          "code"=> "F002",
          "name"=> "chicken"
        )
      )
    )
  )
)