drq1257 2018-07-09 05:47 采纳率: 0%
浏览 21
已采纳

Array将数组值合并为键

I am trying to convert an multi-dimentional array of product data where the PayPal email is set as one of the array values(can be same for some). I need help on merging those as arrays where the paypal_email key is same. Here is the print_r of the array that I have.

Array
(
    [0] => Array
        (
            [name] => Test Product
            [product_id] => 307
            [variation_id] => 0
            [subtotal] => 30
            [total] => 30
            [quantity] => 2
            [subtotal_tax] => 2.66
            [total_tax] => 2.66
            [paypal_email] => thisismytestpaypal@sandbox.com
            [vendor_id] => 2
            [shipping] => Array
                (
                    [shipping_total] => 25.00
                    [shipping_tax] => 2.22
                )

        )

    [1] => Array
        (
            [name] => Album
            [product_id] => 354
            [variation_id] => 0
            [subtotal] => 15
            [total] => 15
            [quantity] => 1
            [subtotal_tax] => 1.33
            [total_tax] => 1.33
            [paypal_email] => thisismytestpaypal@sandbox.com
            [vendor_id] => 11
            [shipping] => Array
                (
                    [shipping_total] => 25.00
                    [shipping_tax] => 2.22
                )

        )

    [2] => Array
        (
            [name] => Beanie with Logo
            [product_id] => 357
            [variation_id] => 0
            [subtotal] => 18
            [total] => 18
            [quantity] => 1
            [subtotal_tax] => 1.60
            [total_tax] => 1.60
            [paypal_email] => ab2@sandbox.com
            [vendor_id] => 10
            [shipping] => Array
                (
                    [shipping_total] => 25.00
                    [shipping_tax] => 2.22
                )

        )

    [3] => Array
        (
            [name] => Long Sleeve Tee
            [product_id] => 352
            [variation_id] => 0
            [subtotal] => 75
            [total] => 75
            [quantity] => 3
            [subtotal_tax] => 6.66
            [total_tax] => 6.66
            [paypal_email] => ab2@sandbox.com
            [vendor_id] => 2
            [shipping] => Array
                (
                    [shipping_total] => 25.00
                    [shipping_tax] => 2.22
                )

        )

    [4] => Array
        (
            [name] => Polo
            [product_id] => 353
            [variation_id] => 0
            [subtotal] => 80
            [total] => 80
            [quantity] => 4
            [subtotal_tax] => 7.10
            [total_tax] => 7.10
            [paypal_email] => tv3@sandbox.com
            [vendor_id] => 8
            [shipping] => Array
                (
                    [shipping_total] => 25.00
                    [shipping_tax] => 2.22
                )

        )

    [5] => Array
        (
            [name] => Product From Test Vendor 3
            [product_id] => 145
            [variation_id] => 0
            [subtotal] => 60
            [total] => 60
            [quantity] => 3
            [subtotal_tax] => 5.33
            [total_tax] => 5.33
            [paypal_email] => tv3@sandbox.com
            [vendor_id] => 10
            [shipping] => Array
                (
                    [shipping_total] => 25.00
                    [shipping_tax] => 2.22
                )

        )

    [6] => Array
        (
            [name] => Product From Vendor 1
            [product_id] => 143
            [variation_id] => 0
            [subtotal] => 20
            [total] => 20
            [quantity] => 2
            [subtotal_tax] => 1.78
            [total_tax] => 1.78
            [paypal_email] => tv1@sandbox.com
            [vendor_id] => 8
            [shipping] => Array
                (
                    [shipping_total] => 25.00
                    [shipping_tax] => 2.22
                )

        )

)  

I need the array to merged as a multi-dimentional array with the paypal email as key and other data as values.. For example,

Array
(   [thisismytestpaypal@sandbox.com] => Array
        (
        [0] => Array
            (
                [name] => Test Product
                [product_id] => 307
                [variation_id] => 0
                [subtotal] => 30
                [total] => 30
                [quantity] => 2
                [subtotal_tax] => 2.66
                [total_tax] => 2.66
                [paypal_email] => thisismytestpaypal@sandbox.com
                [vendor_id] => 2
                [shipping] => Array
                    (
                        [shipping_total] => 25.00
                        [shipping_tax] => 2.22
                    )

            )

        [1] => Array
            (
                [name] => Album
                [product_id] => 354
                [variation_id] => 0
                [subtotal] => 15
                [total] => 15
                [quantity] => 1
                [subtotal_tax] => 1.33
                [total_tax] => 1.33
                [paypal_email] => thisismytestpaypal@sandbox.com
                [vendor_id] => 11
                [shipping] => Array
                    (
                        [shipping_total] => 25.00
                        [shipping_tax] => 2.22
                    )

            )
        )

    [ab2@sandbox.com] => Array
        (
        [0] => Array
            (
                [name] => Beanie with Logo
                [product_id] => 357
                [variation_id] => 0
                [subtotal] => 18
                [total] => 18
                [quantity] => 1
                [subtotal_tax] => 1.60
                [total_tax] => 1.60
                [paypal_email] => ab2@sandbox.com
                [vendor_id] => 10
                [shipping] => Array
                    (
                        [shipping_total] => 25.00
                        [shipping_tax] => 2.22
                    )

            )

        [1] => Array
            (
                [name] => Long Sleeve Tee
                [product_id] => 352
                [variation_id] => 0
                [subtotal] => 75
                [total] => 75
                [quantity] => 3
                [subtotal_tax] => 6.66
                [total_tax] => 6.66
                [paypal_email] => ab2@sandbox.com
                [vendor_id] => 2
                [shipping] => Array
                    (
                        [shipping_total] => 25.00
                        [shipping_tax] => 2.22
                    )

            )
        )

    [tv3@sandbox.com] => Array
        (
        [0] => Array
            (
                [name] => Polo
                [product_id] => 353
                [variation_id] => 0
                [subtotal] => 80
                [total] => 80
                [quantity] => 4
                [subtotal_tax] => 7.10
                [total_tax] => 7.10
                [paypal_email] => tv3@sandbox.com
                [vendor_id] => 8
                [shipping] => Array
                    (
                        [shipping_total] => 25.00
                        [shipping_tax] => 2.22
                    )

            )

        [1] => Array
            (
                [name] => Product From Test Vendor 3
                [product_id] => 145
                [variation_id] => 0
                [subtotal] => 60
                [total] => 60
                [quantity] => 3
                [subtotal_tax] => 5.33
                [total_tax] => 5.33
                [paypal_email] => tv3@sandbox.com
                [vendor_id] => 10
                [shipping] => Array
                    (
                        [shipping_total] => 25.00
                        [shipping_tax] => 2.22
                    )

            )
        )
    [tv1@sandbox.com] => Array
        (
        [0] => Array
            (
                [name] => Product From Vendor 1
                [product_id] => 143
                [variation_id] => 0
                [subtotal] => 20
                [total] => 20
                [quantity] => 2
                [subtotal_tax] => 1.78
                [total_tax] => 1.78
                [paypal_email] => tv1@sandbox.com
                [vendor_id] => 8
                [shipping] => Array
                    (
                        [shipping_total] => 25.00
                        [shipping_tax] => 2.22
                    )

            )
        )

)

How can I do that?
Thanks

  • 写回答

4条回答 默认 最新

  • dougu2240 2018-07-09 05:54
    关注
    $orders = [.....]; //your array
    $mappedOrders = [];
    foreach($orders as $order) {
        $mail = $order['paypal_email'];
        if(!isset($mappedOrders[$mail])) { $mappedOrders[$mail] = []; }  //create new array in map if $mail index not exists
        $mappedOrders[$mail][] = $order;  // push order into array for mapped array
    }
    
    var_dump($mappedOrders);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 php 将rtmp协议转hls协议,无法播放
  • ¥15 miniconda安装不了
  • ¥20 python代码编写
  • ¥20 使用MPI广播数据遇到阻塞
  • ¥15 TinyMCE如何去掉自动弹出的“链接…”工具?
  • ¥15 微信支付转账凭证,如何解决
  • ¥15 在win10下使用指纹登录时,界面上的文字最后一个字产生换行现象
  • ¥20 使用AT89C51微控制器和MAX7219驱动器来实现0到99秒的秒表计数,有开始和暂停以及复位功能,下面有仿真图,请根据仿真图来设计c语言程序
  • ¥15 51单片机 双路ad同步采样
  • ¥15 使用xdocreport 生成word