SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict Shaydez's Avatar
    Join Date
    Jul 2006
    Location
    Boca Raton, Florida
    Posts
    356
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Help with Merging Arrays

    ok I have 2 array values.. That's coming from 2 different methods in my class file.


    This is coming from my Keyword Method
    Code:
    Array
    (
        [4193] => Array
            (
                [Tagged] => Array
                    (
                        [0] => Keratin Treatment
                        [1] => Couples Massage
                    )
    
            )
    
        [3574] => Array
            (
                [Tagged] => Array
                    (
                        [0] => Deep Massage
                    )
    
            )
    
    )
    this is coming from another method

    Code:
    Array
    (
        [4193] => Array
            (
                [salonID] => 4193
                [miles] => 0.283011807234213
                [featured] => 3
            )
    
        [3574] => Array
            (
                [salonID] => 3574
                [miles] => 0.718207507865745
                [featured] => 3
            )
    
    )
    I want to merge the two to look like this

    Code:
    Array
    (
        [4193] => Array
            (
                [salonID] => 4193
                [miles] => 0.283011807234213
                [featured] => 3
                [Tagged] => Array
                    (
                        [0] => Keratin Treatment
                        [1] => Couples Massage
                    )
    
            )
    
        [3574] => Array
            (
                [salonID] => 3574
                [miles] => 0.718207507865745
                [featured] => 3
                [Tagged] => Array
                    (
                        [0] => Deep Massage
                    )
    
            )
    
    )

    I tried the following techniques

    $Results = $arr_value_a+$arr_value_b it resets the keys..
    and other array merging functions provided by php resets the keys or just ends up deleting half the arrays...
    Sr. Website Developer and Internet Marketing
    www.CarlosJa.com Note: If anyone
    needs to get ahold of me please feel free to email me through
    my site. Apparently i missed quite a few private messages.

  2. #2
    SitePoint Addict Shaydez's Avatar
    Join Date
    Jul 2006
    Location
    Boca Raton, Florida
    Posts
    356
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    So i found a function on Stock OverFlow

    which does this

    Code:
    Array
    (
        [4193] => Array
            (
                [0] => Array
                    (
                        [salonID] => 4193
                        [miles] => 0.283011807234213
                        [featured] => 3
                    )
    
                [1] => Array
                    (
                        [Tagged] => Array
                            (
                                [0] => Keratin Treatment
                                [1] => Couples Massage
                            )
    
                    )
    
            )
    
        [3574] => Array
            (
                [0] => Array
                    (
                        [salonID] => 3574
                        [miles] => 0.718207507865745
                        [featured] => 3
                    )
    
                [1] => Array
                    (
                        [Tagged] => Array
                            (
                                [0] => Deep Massage
                            )
    
                    )
    
            )
    
    )
    Almost Perfect... but the problem is i don't want the auto count inside my array.


    This is what i'm using

    PHP Code:
    function merge_common_keys(){
        
    $arr func_get_args();
        
    $num func_num_args();

        
    $keys = array();
        

        
    $i 0;
        for(
    $i=0;$i<$num;++$i){
            
    $keys array_merge($keysarray_keys($arr[$i]));
        }
            
        
    $keys array_unique($keys);
        
        
    $merged = array();

        foreach(
    $keys as $key){
            
    $merged[$key] = array();
            for(
    $i=0;$i<$num;++$i){
                
    $merged[$key][] = isset($arr[$i][$key])?$arr[$i][$key]:null;
            }
        }
        return 
    $merged;

    Can someone help me modify this so it doesn't automatically auto count keys.
    Sr. Website Developer and Internet Marketing
    www.CarlosJa.com Note: If anyone
    needs to get ahold of me please feel free to email me through
    my site. Apparently i missed quite a few private messages.

  3. #3
    SitePoint Addict Shaydez's Avatar
    Join Date
    Jul 2006
    Location
    Boca Raton, Florida
    Posts
    356
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    LMAO!! Nevermind i figured it out .. i just needed to go for a walk. was coding for 12hours straight :\
    Sr. Website Developer and Internet Marketing
    www.CarlosJa.com Note: If anyone
    needs to get ahold of me please feel free to email me through
    my site. Apparently i missed quite a few private messages.

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,411
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi Shaydez,

    Not sure if this is the same solution you arrived at in the end, but array_replace_recursive will also get you the result you want.

  5. #5
    SitePoint Addict Shaydez's Avatar
    Join Date
    Jul 2006
    Location
    Boca Raton, Florida
    Posts
    356
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    array_intersect_key i used.. worked like a charm .
    Sr. Website Developer and Internet Marketing
    www.CarlosJa.com Note: If anyone
    needs to get ahold of me please feel free to email me through
    my site. Apparently i missed quite a few private messages.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •