【问题标题】:Array Merge with array value as key以数组值作为键的数组合并
【发布时间】:2018-07-09 05:47:47
【问题描述】:

我正在尝试转换产品数据的多维数组,其中将 PayPal 电子邮件设置为数组值之一(某些值可能相同)。我需要帮助将它们合并为 paypal_email 键相同的数组。这是我拥有的数组的print_r

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
                )

        )

)  

我需要将数组合并为一个多维数组,其中贝宝电子邮件作为键,其他数据作为值。例如,

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
                    )

            )
        )

)

我该怎么做?
谢谢

【问题讨论】:

  • 到目前为止你有没有尝试过?
  • @NigelRen,是的,我做到了。有太多方法让我迷失了方向:(
  • @Abhik,我知道这种感觉,但不努力会导致对 SO 投反对票,因为它写着:为我编写代码。
  • @Progrock,那不是故意的,只是失去了尝试的轨迹。
  • @Abhik,我确定这不是您的意图,只是警告您,因为您的问题可能会被否决甚至关闭 :)

标签: php arrays multidimensional-array array-merge


【解决方案1】:
$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);

【讨论】:

    【解决方案2】:

    使用数组键检查执行循环

    $result = [];
    
    foreach ($array as $value) {
        $email = $value['paypal_email'];          // Get email
    
        if (!array_key_exists($email, $result)) { // Check if already on the result array
            $result[$email] = [];                 // Create new array with email as key if not
        }
    
        $result[$email][] = $value;               // push value to email key
    }
    
    print_r($result);
    

    【讨论】:

    • hihi 几乎相同的答案......当然在这种情况下,如果 array_key_exists 或 isset 没有关系......
    • 不需要为不存在的键检查和初始化一个空数组。
    • 实际上你是对的 progrock - PHP 中不需要它。但它更好的代码风格和更容易将该逻辑适应其他编程语言
    • @Kapsonfire 与其他编程语言有什么关系?
    【解决方案3】:

    考虑变量$array 持有的第一个数组。然后使用简单的foreach 循环对其进行迭代,并将数据传输到新数组$newArray,并将paypal_email 节点中的键作为值。代码应该是:

    $newArray = [];
    
    foreach ($array as $value) {
        $newArray[$value['paypal_email']][] = $value;
    }
    print_r($newArray);
    

    【讨论】:

    • 不起作用,因为索引可能有多个条目..所以你需要一个数组
    • @Kapsonfire 他们修好了。
    【解决方案4】:
    <?php
    
    $data = 
    [
        [
            'name' => 'John',
            'email' => 'thebeatles@example.com'
        ],
        [
            'name' => 'Ringo',
            'email' => 'thebeatles@example.com'
        ],
        [
            'name' => 'Mick',
            'email' => 'thestones@example.com'
        ],
        [
            'name' => 'Keith',
            'email' => 'thestones@example.com'
        ],
    ];
    $grouped_by_email = [];
    foreach($data as $item)
        $grouped_by_email[$item['email']][] = $item;
    
    var_export($grouped_by_email);
    

    输出:

    array (
        'thebeatles@example.com' => 
        array (
        0 => 
        array (
            'name' => 'John',
            'email' => 'thebeatles@example.com',
        ),
        1 => 
        array (
            'name' => 'Ringo',
            'email' => 'thebeatles@example.com',
        ),
        ),
        'thestones@example.com' => 
        array (
        0 => 
        array (
            'name' => 'Mick',
            'email' => 'thestones@example.com',
        ),
        1 => 
        array (
            'name' => 'Keith',
            'email' => 'thestones@example.com',
        ),
        ),
    )
    

    【讨论】:

      猜你喜欢
      • 2015-07-02
      • 1970-01-01
      • 2017-02-25
      • 2021-07-12
      • 1970-01-01
      • 2017-06-26
      • 2012-09-11
      • 1970-01-01
      • 2016-11-18
      相关资源
      最近更新 更多