Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

mapping two multidimensional array in php

Posted on 2014-09-23
5
Medium Priority
?
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 12

Assisted Solution

by:Radek Baranowski
Radek Baranowski earned 800 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

670 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