简体   繁体   中英

php how to grouping by id

I'm very new in PHP. I'm doing with the array data structure. Currently, I got two result which is the same ID but different details. I need to combine the details based on the ID.

This is my array result

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"
      ]
    ]
  ]
]

This is my expected result

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"
      ]
    ]
  ]
]

I'm trying to insert the order array if there have the same id

This is my code

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; 
    }
}

Result

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

As you can see, I grouped the order but the date and id were missing.

Assign the proper data. Try -

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'],
    ];
}

I would write something like this, if you need to account for multiple IDs:

$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;
}, [] );

Here is a working order function:

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);
}

INPUT:

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"
      )
    )
  )
);

OUTPUT:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM