回显从查询检索到基于相同值的表PHP的数据

So I have this array that consists of

array (size=3)
  0 => 
   array (size=2)
    'id' => string '46' (length=2)
    'ship_product_code' => string '122' (length=6)
    'purchase_order_number' => string 'PO-1' (length=4)
  1 => 
   array (size=2)
    'id' => string '47' (length=2)
    'ship_product_code' => string '123' (length=6)
    'purchase_order_number' => string 'PO-2' (length=4)
  2 => 
   array (size=2)
    'id' => string '50' (length=2)
    'ship_product_code' => string '124' (length=6)
    'purchase_order_number' => string 'PO-2' (length=4)

I wanted to echo a table that is based on the Purchase Order Number. So by given above array, how can I create a table that is like following:

  PO-1 //PO Number
  122 // Product Code

  <tr> <td> </td> </tr>//another td for space 

  PO-2 //PO Number
  123 // Product Code
  124 // Product Code

How can I "group" the result based on the Purchase Order Number?? Have been dealing with it quite a while, keep searching, but can't really find any resource to achieve so.

I have following code that work but not perfectly

  if(count($products)>0){
            $i=0;
            foreach($products as $v){
                $PDFCONTENT .= '<tr> 
                    <td align="center" width="7%"> </td>
                    <td align="center" width="50%">'.$v['purchase_order_number'].'</td>
                </tr>';
                $i++;
                $PDFCONTENT .= '
                    <tr>
                        <td align="center" width="7%">'.$i.'</td>
                        <td align="center" width="50%">'.$v['ship_product_code].'</td>
                        <td align="center" width="7%">'.number_format($v['qty']).'</td>
                        <td width="7%"> </td>
                    </tr>
                    <tr>
                        <td></td>
                    </tr>
                ';
            }
        }

above code will produce following table

 PO-1 
 122

 PO-2
 123

 PO-2
 124

Please note that I edited the array data here, so I can be more straightforward. There is no problem in outputting the data. Only how can I group the based on the PO Number.

Thank you so much for the help :)

Use following code snippet. With same PO Number we have now only one record.

$fianlProducts = [];
if(count($products)>0){
        foreach($products as $row){
                $fianlProducts[$row['purchase_order_number']]['purchase_order_number'] = $row['purchase_order_number'];
                $fianlProducts[$row['purchase_order_number']]['ship_product_code'][] = $row;
        }
        $i=0;
        foreach($fianlProducts as $v){
                $PDFCONTENT .= '<tr> 
                    <td align="center" width="7%"> </td>
                    <td align="center" width="50%">'.$v['purchase_order_number'].'</td>
                </tr>';
                foreach($v['ship_product_code'] as $w){
                        $i++;
                        $PDFCONTENT .= '
                            <tr>
                                <td align="center" width="7%">'.$i.'</td>
                                <td align="center" width="50%">'.$w['ship_product_code'].'</td>
                                <td align="center" width="7%">'.number_format($w['qty']).'</td>
                                <td width="7%"> </td>
                            </tr>
                            <tr>
                                <td></td>
                            </tr>
                        ';
                }                
        }
}

What you may want to consider is to store the data in a relational database so that you could group by any number of fields.

If your PO number is unique (meaning that there will never be two identical PO numbers), you could use that as your Primary Key for a table that relates to one or more products via a relational table.

You'd have a table with PO Numbers as the primary key:

PO Num |
1

And a table that relates them to products

PO Num Product ID | PO Num | Product ID
1                   1        1

And then finally a Product Table that can store stuff about your products

Product ID | Product Name
1            Widget

From here, it's just querying based on a PO Num: SELECT * FROM PO_Table LEFT JOIN PO_Products_Table ON PO_Table.PO Num = PO_Products_Table.PO Num LEFT JOIN Products_Table ON PO_Products_Table.ProductID = Products_table.ProductID;

SQL has tons of built in functionality, for grouping, ect.

If you'd like to learn more about this potential option, I'd recommend learning MySQL, there are some great resources both on stack overflow and w3c.

I wouldn't really recommend storing data like this in php only. There's potential for introducing errors every time you need to update the data inside your code; as a rule of thumb, you should always separate your data and the code that performs actions on that data.

You need to group it before displaying :

$newData = array();

foreach($data as $row){
    $purchaseOrderNumber = $row['purchaseOrderNumber'];

    if(!isset($newData[$purchaseOrderNumber])){
        $newData[$purchaseOrderNumber] = array(
            'ship_product_code' => array(),
        );
    }

    $newData[$purchaseOrderNumber]['ship_product_code'][] = $row['ship_product_code'];
}

It should produce this array :

$newData = array(
    'PO-1' => array(
        'ship_product_code' => array(
            122,
        ),
    ),

    'PO-2' => array(
        'ship_product_code' => array(
            123,
            124,
        ),
    ),
);

UPDATE:

Then you can display it this way :

foreach($newData as $purchaseOrderCode => $row){
    echo $purchaseOrderCode .' : ' . implode(',', $row['ship_product_code']);
}

Which produces this output :

PO-1 : 122
PO-2 : 123,124