格式化使用PHP数组 [英] formatting an array using php

查看:131
本文介绍了格式化使用PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下阵列具有重复数据:

I have the following array which have duplicate data:

Array
(
    [0] => Array
        (
            [orders] => Array
                (
                    [id] => 9
                    [name] => Abdus Sattar Bhuiyan
                    [email] => sattar.kuet@gmail.com
                    [mobile] => 01673050495
                    [alt_mobile] => 01818953250
                    [city_id] => 2
                    [location_id] => 5
                    [status] => No contact
                    [chashed] => NO
                    [created] => 2015-06-27 12:49:34
                    [modified] => 2015-06-27 12:49:34
                    [comment] => 
                )

            [order_products] => Array
                (
                    [id] => 2
                    [order_id] => 9
                    [product_id] => 1
                    [pieces] => 1
                )

            [products] => Array
                (
                    [id] => 1
                    [category_id] => 1
                    [name] => নভোযানের নাম সি প্রোগ্রামিং
                    [writer] => Engr. Abdus Sattar Bhuiyan
                    [created] => 2015-06-24 16:17:45
                )

            [psettings] => Array
                (
                    [id] => 1
                    [category_id] => 1
                    [product_id] => 1
                    [img] => 1.jpg
                    [desc] => description
                    [created] => 2015-06-28 00:28:26
                    [bppp] => 44000
                    [sppp] => 45000
                    [discount] => 25
                    [service_charge] => 30
                )

        )

    [1] => Array
        (
            [orders] => Array
                (
                    [id] => 10
                    [name] => Abdus Sattar Bhuiyan
                    [email] => sattar.kuet@gmail.com
                    [mobile] => 01673050495
                    [alt_mobile] => 01818953250
                    [city_id] => 1
                    [location_id] => 1
                    [status] => No contact
                    [chashed] => NO
                    [created] => 2015-06-28 03:30:25
                    [modified] => 2015-06-28 03:30:25
                    [comment] => 
                )

            [order_products] => Array
                (
                    [id] => 6
                    [order_id] => 10
                    [product_id] => 1
                    [pieces] => 1
                )

            [products] => Array
                (
                    [id] => 1
                    [category_id] => 1
                    [name] => নভোযানের নাম সি প্রোগ্রামিং
                    [writer] => Engr. Abdus Sattar Bhuiyan
                    [created] => 2015-06-24 16:17:45
                )

            [psettings] => Array
                (
                    [id] => 1
                    [category_id] => 1
                    [product_id] => 1
                    [img] => 1.jpg
                    [desc] => description
                    [created] => 2015-06-28 00:28:26
                    [bppp] => 44000
                    [sppp] => 45000
                    [discount] => 25
                    [service_charge] => 30
                )

        )

    [2] => Array
        (
            [orders] => Array
                (
                    [id] => 9
                    [name] => Abdus Sattar Bhuiyan
                    [email] => sattar.kuet@gmail.com
                    [mobile] => 01673050495
                    [alt_mobile] => 01818953250
                    [city_id] => 2
                    [location_id] => 5
                    [status] => No contact
                    [chashed] => NO
                    [created] => 2015-06-27 12:49:34
                    [modified] => 2015-06-27 12:49:34
                    [comment] => 
                )

            [order_products] => Array
                (
                    [id] => 3
                    [order_id] => 9
                    [product_id] => 2
                    [pieces] => 1
                )

            [products] => Array
                (
                    [id] => 2
                    [category_id] => 1
                    [name] => Resonance of creativity with C++
                    [writer] => Engr. Abdus Sattar Bhuiyan
                    [created] => 2015-06-26 07:32:52
                )

            [psettings] => Array
                (
                    [id] => 2
                    [category_id] => 1
                    [product_id] => 2
                    [img] => 2.jpg
                    [desc] => 
                    [created] => 2015-06-26 07:33:41
                    [bppp] => 150
                    [sppp] => 250
                    [discount] => 20
                    [service_charge] => 30
                )

        )

    [3] => Array
        (
            [orders] => Array
                (
                    [id] => 10
                    [name] => Abdus Sattar Bhuiyan
                    [email] => sattar.kuet@gmail.com
                    [mobile] => 01673050495
                    [alt_mobile] => 01818953250
                    [city_id] => 1
                    [location_id] => 1
                    [status] => No contact
                    [chashed] => NO
                    [created] => 2015-06-28 03:30:25
                    [modified] => 2015-06-28 03:30:25
                    [comment] => 
                )

            [order_products] => Array
                (
                    [id] => 5
                    [order_id] => 10
                    [product_id] => 2
                    [pieces] => 1
                )

            [products] => Array
                (
                    [id] => 2
                    [category_id] => 1
                    [name] => Resonance of creativity with C++
                    [writer] => Engr. Abdus Sattar Bhuiyan
                    [created] => 2015-06-26 07:32:52
                )

            [psettings] => Array
                (
                    [id] => 2
                    [category_id] => 1
                    [product_id] => 2
                    [img] => 2.jpg
                    [desc] => 
                    [created] => 2015-06-26 07:33:41
                    [bppp] => 150
                    [sppp] => 250
                    [discount] => 20
                    [service_charge] => 30
                )

        )

    [4] => Array
        (
            [orders] => Array
                (
                    [id] => 9
                    [name] => Abdus Sattar Bhuiyan
                    [email] => sattar.kuet@gmail.com
                    [mobile] => 01673050495
                    [alt_mobile] => 01818953250
                    [city_id] => 2
                    [location_id] => 5
                    [status] => No contact
                    [chashed] => NO
                    [created] => 2015-06-27 12:49:34
                    [modified] => 2015-06-27 12:49:34
                    [comment] => 
                )

            [order_products] => Array
                (
                    [id] => 4
                    [order_id] => 9
                    [product_id] => 3
                    [pieces] => 1
                )

            [products] => Array
                (
                    [id] => 3
                    [category_id] => 1
                    [name] => programming by story C
                    [writer] => Hasibul Hasan Shanto
                    [created] => 2015-06-26 07:35:57
                )

            [psettings] => Array
                (
                    [id] => 3
                    [category_id] => 1
                    [product_id] => 3
                    [img] => 3.jpg
                    [desc] => 
                    [created] => 2015-06-26 07:36:26
                    [bppp] => 150
                    [sppp] => 250
                    [discount] => 10
                    [service_charge] => 30
                )

        )

    [5] => Array
        (
            [orders] => Array
                (
                    [id] => 10
                    [name] => Abdus Sattar Bhuiyan
                    [email] => sattar.kuet@gmail.com
                    [mobile] => 01673050495
                    [alt_mobile] => 01818953250
                    [city_id] => 1
                    [location_id] => 1
                    [status] => No contact
                    [chashed] => NO
                    [created] => 2015-06-28 03:30:25
                    [modified] => 2015-06-28 03:30:25
                    [comment] => 
                )

            [order_products] => Array
                (
                    [id] => 7
                    [order_id] => 10
                    [product_id] => 3
                    [pieces] => 1
                )

            [products] => Array
                (
                    [id] => 3
                    [category_id] => 1
                    [name] => programming by story C
                    [writer] => Hasibul Hasan Shanto
                    [created] => 2015-06-26 07:35:57
                )

            [psettings] => Array
                (
                    [id] => 3
                    [category_id] => 1
                    [product_id] => 3
                    [img] => 3.jpg
                    [desc] => 
                    [created] => 2015-06-26 07:36:26
                    [bppp] => 150
                    [sppp] => 250
                    [discount] => 10
                    [service_charge] => 30
                )

        )

)

我想格式化这个阵列,并产生以下数组:

I want to format this array and produce the following array:

Array
(
    [0] => Array
        (
            [orders] => Array
                (
                    [id] => 9
                    [name] => Abdus Sattar Bhuiyan
                    [email] => sattar.kuet@gmail.com
                    [mobile] => 01673050495
                    [alt_mobile] => 01818953250
                    [city_id] => 2
                    [location_id] => 5
                    [status] => No contact
                    [chashed] => NO
                    [created] => 2015-06-27 12:49:34
                    [modified] => 2015-06-27 12:49:34
                    [comment] => 
                )

            [order_products] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [order_id] => 9
                            [product_id] => 1
                            [pieces] => 1
                        )

                    [1] => Array
                        (
                            [id] => 3
                            [order_id] => 9
                            [product_id] => 2
                            [pieces] => 1
                        )

                    [2] => Array
                        (
                            [id] => 4
                            [order_id] => 9
                            [product_id] => 3
                            [pieces] => 1
                        )

                )

            [products] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [category_id] => 1
                            [name] => নভোযানের নাম সি প্রোগ্রামিং
                            [writer] => Engr. Abdus Sattar Bhuiyan
                            [created] => 2015-06-24 16:17:45
                        )

                    [1] => Array
                        (
                            [id] => 2
                            [category_id] => 1
                            [name] => Resonance of creativity with C++
                            [writer] => Engr. Abdus Sattar Bhuiyan
                            [created] => 2015-06-26 07:32:52
                        )

                    [2] => Array
                        (
                            [id] => 3
                            [category_id] => 1
                            [name] => programming by story C
                            [writer] => Hasibul Hasan Shanto
                            [created] => 2015-06-26 07:35:57
                        )

                )

            [psettings] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [category_id] => 1
                            [product_id] => 1
                            [img] => 1.jpg
                            [desc] => description
                            [created] => 2015-06-28 00:28:26
                            [bppp] => 44000
                            [sppp] => 45000
                            [discount] => 25
                            [service_charge] => 30
                        )

                    [1] => Array
                        (
                            [id] => 2
                            [category_id] => 1
                            [product_id] => 2
                            [img] => 2.jpg
                            [desc] => 
                            [created] => 2015-06-26 07:33:41
                            [bppp] => 150
                            [sppp] => 250
                            [discount] => 20
                            [service_charge] => 30
                        )

                    [2] => Array
                        (
                            [id] => 3
                            [category_id] => 1
                            [product_id] => 3
                            [img] => 3.jpg
                            [desc] => 
                            [created] => 2015-06-26 07:36:26
                            [bppp] => 150
                            [sppp] => 250
                            [discount] => 10
                            [service_charge] => 30
                        )

                )

        )

    [1] => Array
        (
            [orders] => Array
                (
                    [id] => 10
                    [name] => Abdus Sattar Bhuiyan
                    [email] => sattar.kuet@gmail.com
                    [mobile] => 01673050495
                    [alt_mobile] => 01818953250
                    [city_id] => 1
                    [location_id] => 1
                    [status] => No contact
                    [chashed] => NO
                    [created] => 2015-06-28 03:30:25
                    [modified] => 2015-06-28 03:30:25
                    [comment] => 
                )

            [order_products] => Array
                (
                     [0] => Array
                        ( [id] => 6
                           [order_id] => 10
                           [product_id] => 1
                           [pieces] => 1
                       )
                    [1] => Array
                        (
                            [id] => 5
                            [order_id] => 10
                            [product_id] => 2
                            [pieces] => 1
                        )

                    [2] => Array
                        (
                            [id] => 7
                            [order_id] => 10
                            [product_id] => 3
                            [pieces] => 1
                        )

                )

            [products] => Array
                (
                   [0] => Array
                        (
                            [id] => 1
                            [category_id] => 1
                            [name] => নভোযানের নাম সি প্রোগ্রামিং
                            [writer] => Engr. Abdus Sattar Bhuiyan
                            [created] => 2015-06-24 16:17:45
                   )
                    [1] => Array
                        (
                            [id] => 2
                            [category_id] => 1
                            [name] => Resonance of creativity with C++
                            [writer] => Engr. Abdus Sattar Bhuiyan
                            [created] => 2015-06-26 07:32:52
                        )

                    [2] => Array
                        (
                            [id] => 3
                            [category_id] => 1
                            [name] => programming by story C
                            [writer] => Hasibul Hasan Shanto
                            [created] => 2015-06-26 07:35:57
                        )

                )

            [psettings] => Array
                (
                  [0] => Array
                        (
                    [id] => 1
                    [category_id] => 1
                    [product_id] => 1
                    [img] => 1.jpg
                    [desc] => description
                    [created] => 2015-06-28 00:28:26
                    [bppp] => 44000
                    [sppp] => 45000
                    [discount] => 25
                    [service_charge] => 30
                    )
                    [1] => Array
                        (
                            [id] => 2
                            [category_id] => 1
                            [product_id] => 2
                            [img] => 2.jpg
                            [desc] => 
                            [created] => 2015-06-26 07:33:41
                            [bppp] => 150
                            [sppp] => 250
                            [discount] => 20
                            [service_charge] => 30
                        )

                    [2] => Array
                        (
                            [id] => 3
                            [category_id] => 1
                            [product_id] => 3
                            [img] => 3.jpg
                            [desc] => 
                            [created] => 2015-06-26 07:36:26
                            [bppp] => 150
                            [sppp] => 250
                            [discount] => 10
                            [service_charge] => 30
                        )

                )

        )


)  

我怎样才能做到这一点。我重新简单数组,但这没有任何意义给我的格式。这让我哭了。请帮帮我。如果建议任何辅助函数这​​将是一个真正的礼物。提前致谢。

How can I do this. I reformat simple array but this does not make sense to me to format. It makes me cry. Please help me. If any helper function is suggested it will be really a gift. Thanks in advance.

推荐答案

您需要做的象下面这样: -

You need to do like below:-

<?php
$firstArray = Array
(
    '0' => Array
        (
            'orders' => Array
                (
                    'id' => 9,
                    'name' => 'Abdus Sattar Bhuiyan',
                    'email' => 'sattar.kuet@gmail.com',
                    'mobile' => '01673050495',
                    'alt_mobile' => '01818953250',
                    'city_id' => 2,
                    'location_id' => 5,
                    'status' => 'No contact',
                    'chashed' => 'NO',
                    'created' => '2015-06-27 12:49:34',
                    'modified' => '2015-06-27 12:49:34',
                    'comment' => ''
                ),
            'order_products' => Array
                (
                    'id' => 2,
                    'order_id' => 9,
                    'product_id' => 1,
                    'pieces' => 1
                ),
            'products' => Array
                (
                    'id' => 1,
                    'category_id' => 1,
                    'name' => 'নভোযানের নাম সি প্রোগ্রামিং',
                    'writer' => 'Engr. Abdus Sattar Bhuiyan',
                    'created' => '2015-06-24 16:17:45'
                ),
            'psettings' => Array
                (
                    'id' => 1,
                    'category_id' => 1,
                    'product_id' => 1,
                    'img' => '1.jpg',
                    'desc' => 'description',
                    'created' => '2015-06-28 00:28:26',
                    'bppp' => 44000,
                    'sppp' => 45000,
                    'discount' => 25,
                    'service_charge' => 30
                ),
        ),
    '1' => Array
        (
            'orders' => Array
                (
                    'id' => 9,
                    'name' => 'Abdus Sattar Bhuiyan',
                    'email' => 'sattar.kuet@gmail.com',
                    'mobile' => '01673050495',
                    'alt_mobile' => '01818953250',
                    'city_id' => 2,
                    'location_id' => 5,
                    'status' => 'No contact',
                    'chashed' => 'NO',
                    'created' => '2015-06-27 12:49:34',
                    'modified' => '2015-06-27 12:49:34',
                    'comment' => ''
                ),
            'order_products' => Array
                (
                    'id' => 3,
                    'order_id' => 9,
                    'product_id' => 2,
                    'pieces' => 1
                ),
            'products' => Array
                (
                    'id' => 2,
                    'category_id' => 1,
                    'name' => 'Resonance of creativity with C++',
                    'writer' => 'Engr. Abdus Sattar Bhuiyan',
                    'created' => '2015-06-26 07:32:52'
                ),
            'psettings' => Array
                (
                    'id' => 2,
                    'category_id' => 1,
                    'product_id' => 2,
                    'img' => '2.jpg',
                    'desc' => '',
                    'created' => '2015-06-26 07:33:41',
                    'bppp' => 150,
                    'sppp' => 250,
                    'discount' => 20,
                    'service_charge' => 30
                ),
        ),
    '2' => Array
        (
            'orders' => Array
                (
                    'id' => 9,
                    'name' => 'Abdus Sattar Bhuiyan',
                    'email' => 'sattar.kuet@gmail.com',
                    'mobile' => '01673050495',
                    'alt_mobile' => '01818953250',
                    'city_id' => 2,
                    'location_id' => 5,
                    'status' => 'No contact',
                    'chashed' => 'NO',
                    'created' => '2015-06-27 12:49:34',
                    'modified' => '2015-06-27 12:49:34',
                    'comment' => ''
                ),
            'order_products' => Array
                (
                    'id' => 4,
                    'order_id' => 9,
                    'product_id' => 3,
                    'pieces' => 1
                ),
            'products' => Array
                (
                    'id' => 3,
                    'category_id' => 1,
                    'name' => 'programming by story C',
                    'writer' => 'Hasibul Hasan Shanto',
                    'created' => '2015-06-26 07:35:57'
                ),
            'psettings' => Array
                (
                    'id' => 3,
                    'category_id' => 1,
                    'product_id' => 3,
                    'img' => '3.jpg',
                    'desc' => '',
                    'created' => '2015-06-26 07:36:26',
                    'bppp' => 150,
                    'sppp' => 250,
                    'discount' => 10,
                    'service_charge' => 30
                ),
        ),
);
$new_array = array();

foreach($firstArray as $key=> $arr){
    if($key == 0){
        $new_array[0]['orders'] = $arr['orders'];
        $new_array[0]['order_products'][] = $arr['order_products'];
        $new_array[0]['products'][] = $arr['products'];
        $new_array[0]['psettings'][] = $arr['psettings'];
    }else{
         foreach($new_array as $key=> $newarr){
              if($arr['orders'] == $newarr['orders']){
                $new_array[$key]['order_products'][] = $arr['order_products'];
                $new_array[$key]['products'][] = $arr['products'];
                $new_array[$key]['psettings'][] = $arr['psettings'];
              }else{
                  $new_array[] = $arr;
              }
         }
    }

}
echo "<pre/>";print_r($new_array);
?>

输出: - 。 https://eval.in/388565

请注意: - 不要担心我的推杆数组。我只是把你的阵列和格式化,它运行的目的,并在我的最终检查。谢谢。

Note:- don't worry about the array i putted. I just take your array and formatted it for running purpose and checking at my end. thanks.

这篇关于格式化使用PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆