Solved

mapping two multidimensional array in php

Posted on 2014-09-23
5
159 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 count occurrences of each item in an array.

758 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now