Solved

mapping two multidimensional array in php

Posted on 2014-09-23
5
168 Views
Last Modified: 2014-09-28
I have the following 2 separates arrays and I want to add one array let say [7] element to the 2nd array if their element [1] match the [email] array value of in 1st array.Any help appreciated

1st arr=

Array
    (
        [0] => Array
            (
                [email] => blanklou@gmail.com
                [media_id] => 50
                [total_earned] => 13.15
            )

        [1] => Array
            (
                [email] => how.jimmy@gmail.com
                [media_id] => 50
                [total_earned] => 6.14
            )

        [2] => Array
            (
                [email] => huatheng_t@hotmail.com
                [media_id] => 50
                [total_earned] => 15.76
            )

        [3] => Array
            (
                [email] => siljackwong@hotmail.com
                [media_id] => 50
                [total_earned] => 11.46
            )

        [4] => Array
            (
                [email] => thestartest@gmail.com
                [media_id] => 50
                [total_earned] => 1.49
            )

        [5] => Array
            (
                [email] => tputra.utama@gmail.com
                [media_id] => 50
                [total_earned] => 0.45
            )

    )

Open in new window

2nd arr =

Array
    (
        [0] => Array
            (
                [0] => 8
                [1] => blanklou@gmail.com
                [2] => 
                [3] => Tay
                [4] => 0.00
                [5] => 2014-09-11 21:12:44
                [6] => 2014-09-15 07:02:51
            )

        [1] => Array
            (
                [0] => 9
                [1] => how.jimmy@gmail.com
                [2] => 
                [3] => How
                [4] => 0.00
                [5] => 2014-09-11 21:16:45
                [6] => 2014-09-23 14:58:56
            )

        [2] => Array
            (
                [0] => 12
                [1] => huatheng_t@hotmail.com
                [2] => Huat
                [3] => Heng
                [4] => 1.00
                [5] => 2014-09-12 13:04:59
                [6] => 2014-09-15 11:12:36
            )

        [3] => Array
            (
                [0] => 13
                [1] => eldy8888@gmail.com
                [2] => Voon
                [3] => Ming Hann
                [4] => 0.00
                [5] => 2014-09-12 16:20:44
                [6] => 2014-09-15 09:22:38
            )

        [4] => Array
            (
                [0] => 15
                [1] => huatheng@shopstylers.com
                [2] => Heng
                [3] => Tang
                [4] => 5.00
                [5] => 2014-09-13 00:23:03
                [6] => 2014-09-13 10:51:44
            )

        [5] => Array
            (
                [0] => 17
                [1] => siljackwong@hotmail.com
                [2] => Siljack
                [3] => Wong Kok Kitt
                [4] => 0.00
                [5] => 2014-09-15 04:27:36
                [6] => 2014-09-15 09:03:04
            )

        [6] => Array
            (
                [0] => 18
                [1] => ja227@hotmail.com
                [2] => Ju An
                [3] => Tan
                [4] => 0.00
                [5] => 2014-09-15 05:05:44
                [6] => 2014-09-15 08:19:56
            )

        [7] => Array
            (
                [0] => 19
                [1] => sakai967@hotmail.com
                [2] => Tan
                [3] => Yi Hoong
                [4] => 0.00
                [5] => 2014-09-15 07:51:46
                [6] => 2014-09-15 08:06:32
            )

        [8] => Array
            (
                [0] => 20
                [1] => thestartest@gmail.com
                [2] => Heng
                [3] => Thestar
                [4] => 0.00
                [5] => 2014-09-23 12:15:11
                [6] => 2014-09-23 14:27:07
            )

        [9] => Array
            (
                [0] => 21
                [1] => chookarchun100@gmail.com
                [2] => Choo
                [3] => Chun
                [4] => 0.00
                [5] => 2014-09-23 12:19:13
                [6] => 2014-09-23 12:19:13
            )

    )

Open in new window

I tried with this code but no luck :

foreach($users_all as $ukey => $usr){

        $totalearned = in_array($usr[1],$sumsales_all);
        var_dump(__LINE__,$usr[1], $totalearned);

    }

Open in new window

0
Comment
Question by:heng tang
  • 4
5 Comments
 
LVL 11

Assisted Solution

by:Radek Baranowski
Radek Baranowski earned 200 total points
ID: 40338770
i'd say

foreach ($arr1 as $arr1item) {
        foreach ($arr2 as $arr2item) {
                if ($arr2item[1] == $arr1item['email']) $arr1item['total_earned']+=$arr2item[4];
   }
}

Open in new window


do I get it right that you want to get a person from arr1, then check if arr2 contains this person and if yes, then add the amount from field [4] of arr2 to the total_earned amount in arr1 for that person ?
0
 

Author Comment

by:heng tang
ID: 40338795
No Radek,

I want to get the email from arr1, then check if arr2 contains this email and if yes, then add the [total_earned] of arr1 to the new element on arr1 for that email.

This is my expected result on arr2:

Array
    (
        [0] => Array
            (
                [0] => 8
                [1] => blanklou@gmail.com
                [2] => 
                [3] => Tay
                [4] => 0.00
                [5] => 2014-09-11 21:12:44
                [6] => 2014-09-15 07:02:51
                [7] => 13.15
            )
        [1] => Array
            (
                [0] => 9
                [1] => how.jimmy@gmail.com
                [2] => 
                [3] => How
                [4] => 0.00
                [5] => 2014-09-11 21:16:45
                [6] => 2014-09-23 14:58:56
                [7] => 6.14
            )
            .
            .
            .
            .
    )

Open in new window

0
 

Author Comment

by:heng tang
ID: 40338796
Actually I prefer to get email from arr2[1] then compare with arr1[email]
0
 

Accepted Solution

by:
heng tang earned 0 total points
ID: 40338803
OK, i figured it out already. I use the following  code :
foreach($users_all as $ukey => $usr){
        
        foreach($sumsales_all as $skey => $sale){
            $match_email = in_array($usr[1],$sale);
            if($match_email){
                
                $users_all[$ukey][7] = (string)$sale['total_earned'];
            }
            
            
        }
        
    }

Open in new window

0
 

Author Closing Comment

by:heng tang
ID: 40348390
i found the solution
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question