php foreach array traversal problem
PHP中文网
PHP中文网 2017-05-16 13:03:11
0
6
603

< /p>

As shown in the figure, how to make the same numbers in order_id only be output once in a loop

The business is like this. This table records the relationship between an order and a product. order_id is the order id.
Therefore, the two 815s recorded in the table indicate that they are the same order. They have the same order id

Then the question is how to merge the same order_id into one output when displaying the order list

The above picture shows the separate output of the same order_id and how to merge them

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(6)
小葫芦
$user_orders = 你获取的数据库数据;
$order_array = array();
foreach($user_orders as $value){
    $order_array[$value['order_id']][] = something;//一些你想放进去的数据 
}
return $order_array;

Output the array according to the order ID, and directly merge the same ones (please tell me if there is anything wrong)

巴扎黑

This has nothing to do with your foreach loop and everything to do with the logic of rendering the page.

For example: when you find that there is already the same order number before, it will be ok if you no longer display the order number

小葫芦

There is a problem with the table structure. Generally, there is an order table and an order product table. For your current table structure, you can first query group by order_id and then foreach order_id and then loop to query all the products in this order_id

某草草

This should be the result you want, process it and then iterate.

$arr = array(
            array('pro_id'=>1,'order_id'=>1),
            array('pro_id'=>2,'order_id'=>1),
            array('pro_id'=>3,'order_id'=>2),
            array('pro_id'=>4,'order_id'=>3),
            array('pro_id'=>5,'order_id'=>4),
        );
$arr = array_column($arr,null,'order_id');
print_r($arr);die;
//打印结果如下
Array
(
    [1] => Array
        (
            [pro_id] => 2
            [order_id] => 1
        )

    [2] => Array
        (
            [pro_id] => 3
            [order_id] => 2
        )

    [3] => Array
        (
            [pro_id] => 4
            [order_id] => 3
        )

    [4] => Array
        (
            [pro_id] => 5
            [order_id] => 4
        )

)
左手右手慢动作

Remove duplicates when fetching the database

phpcn_u1582

select dinstinct(order_id)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template