Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shifoodew's avatar

How to merge an array IF....

Good day, How can I merge an array if the po_id is the same with the other row object?

for example:

[
    {
      "po_id": 42,
      "full_name": "Sample Zample",
      "product_name": "School Owner Manual Payment School Owner Manual Payment",
      "level": "school",
      "date_purchase": "2018-08-09 14:53:35",
      "student_name": "Sample Student1",
      "item_status": 2
    },
    {
      "po_id": 42,
      "full_name": "Sample Zample",
      "product_name": "School Owner Manual Payment School Owner Manual Payment",
      "level": "school",
      "date_purchase": "2018-08-09 14:53:35",
      "student_name": "Sample Student2",
      "item_status": 2
    },
    {
      "po_id": 42,
      "full_name": "Sample Zample",
      "product_name": "School Owner Manual Payment School Owner Manual Payment",
      "level": "school",
      "date_purchase": "2018-08-09 14:53:35",
      "student_name": "Sample Student3",
      "item_status": 2
    },
    {
      "po_id": 43,
      "full_name": "Zample Sample",
      "product_name": "School Owner Manual Payment School Owner Manual Payment",
      "level": "school",
      "date_purchase": "2018-08-09 14:53:35",
      "student_name": "Sample Student1",
      "item_status": 2
    },
]

desired output should look like this.

[
    {
        "po_id": 42,
        "full_name": "Sample Zample",
        "product_name": "School Owner Manual Payment School Owner Manual Payment",
        "level": "school",
        "date_purchase": "2018-08-09 14:53:35",
        "student_name": [
            "Sample Student1",
            "Sample Student2",
            "Sample Student3"
        ],
        "item_status": [
            2,
            3,
            4
        ]
    },
    {
        "po_id": 43,
        "full_name": "Zample Sample",
        "product_name": "School Owner Manual Payment School Owner Manual Payment",
        "level": "school",
        "date_purchase": "2018-08-09 14:53:35",
        "student_name": "Sample Student",
        "item_status": 2
    }
]
0 likes
0 replies

Please or to participate in this conversation.