0

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] => [email protected]
            [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] => [email protected]
            [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] => [email protected]
            [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] => [email protected]
            [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] => [email protected]
            [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] => [email protected]
            [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] => [email protected]
            [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
(   [[email protected]] => 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] => [email protected]
                [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] => [email protected]
                [vendor_id] => 11
                [shipping] => Array
                    (
                        [shipping_total] => 25.00
                        [shipping_tax] => 2.22
                    )

            )
        )

    [[email protected]] => 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] => [email protected]
                [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] => [email protected]
                [vendor_id] => 2
                [shipping] => Array
                    (
                        [shipping_total] => 25.00
                        [shipping_tax] => 2.22
                    )

            )
        )

    [[email protected]] => 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] => [email protected]
                [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] => [email protected]
                [vendor_id] => 10
                [shipping] => Array
                    (
                        [shipping_total] => 25.00
                        [shipping_tax] => 2.22
                    )

            )
        )
    [[email protected]] => 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] => [email protected]
                [vendor_id] => 8
                [shipping] => Array
                    (
                        [shipping_total] => 25.00
                        [shipping_tax] => 2.22
                    )

            )
        )

)

How can I do that?
Thanks

6
  • Have you tried anything so far? Commented Jul 9, 2018 at 5:50
  • @NigelRen, yes I did. There was so many approaches that I lost track :( Commented Jul 9, 2018 at 5:53
  • @Abhik, I know the feeling, but non-demonstration of effort leads to negative voting on SO, as it reads: code this for me. Commented Jul 9, 2018 at 6:09
  • @Progrock, that was not the intention mate, just lost the track of the tries. Commented Jul 9, 2018 at 6:54
  • @Abhik, I'm sure it wasn't your intention, just warning you, as your question may be downvoted or even closed :) Commented Jul 9, 2018 at 8:02

4 Answers 4

3
$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);
Sign up to request clarification or add additional context in comments.

Comments

3

Do a loop with array key checking

$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);

4 Comments

hihi almost same answer... upvote of course in this case it doesnt matter if array_key_exists or isset...
No need to check and initialise an empty array for non existing keys.
actually you are right progrock - there's no need for it in PHP.. but its better codestyle and easier to adapt that logic to other programming languages
@Kapsonfire what have other programming languages got to do with anything?
1

Consider first array hold by variable $array. Then iterate it with simple foreach loop and transfer data to new array $newArray with key as value in paypal_email node. Code should be:

$newArray = [];

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

2 Comments

won't work since an index may have for than one entry.. so you need an array
@Kapsonfire they fixed it.
1
<?php

$data = 
[
    [
        'name' => 'John',
        'email' => '[email protected]'
    ],
    [
        'name' => 'Ringo',
        'email' => '[email protected]'
    ],
    [
        'name' => 'Mick',
        'email' => '[email protected]'
    ],
    [
        'name' => 'Keith',
        'email' => '[email protected]'
    ],
];
$grouped_by_email = [];
foreach($data as $item)
    $grouped_by_email[$item['email']][] = $item;

var_export($grouped_by_email);

Output:

array (
    '[email protected]' => 
    array (
    0 => 
    array (
        'name' => 'John',
        'email' => '[email protected]',
    ),
    1 => 
    array (
        'name' => 'Ringo',
        'email' => '[email protected]',
    ),
    ),
    '[email protected]' => 
    array (
    0 => 
    array (
        'name' => 'Mick',
        'email' => '[email protected]',
    ),
    1 => 
    array (
        'name' => 'Keith',
        'email' => '[email protected]',
    ),
    ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.