Solved

mapping two multidimensional array in php

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

773 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