#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    199
    Rep Power
    29

    Combine two arrays with duplicate keys


    I need to combine two arrays with duplicate keys and keep the duplicates

    My current function works for the part where it's a straight ahead combine with no multi-dimensional arrays (the else in the if/else block)... but as soon as I try to make a mutli-dimensional array my end result is nothing. I end up with an array that is empty.

    PHP Code:
        //Get list of link elements and combine into array
        
    function dataArrayCombine($keylist,$valueslist,$multi=false){
            if(
    $multi===true){
            
    $combinedarray=array();
                foreach(
    $keylist as $ia => $ka) {
                    
    $combinedarray[$ka][]=$valueslist[$ia];
                }
            
    array_walk($combinedarraycreate_function('&$v''$v=(count($v)==1) ? array_pop($v) : $v;'));
            }
            else{
            
    $keyarray=array_map('trim'explode(","$keylist));
            
    $valuesarray=array_map('trim'explode(","$valueslist));
            
    $combinedarray=array_combine($keyarray$valuesarray);
            }
        return 
    $combinedarray;
        } 
    The end result should be something like this:
    Code:
    <?php print_r(dataArrayCombine(Array('a','a','b'), Array(1,2,3),true);?>
    Array
    (
        [a] => Array
            (
                [0] => 1
                [1] => 2
            )
    
        [b] => 3
    )
    Running var_dump on $combinedarray right after the foreach loop is where I detect that the array is empty.
    Last edited by HDFilmMaker2112; August 17th, 2013 at 01:22 PM.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    are you sure you're actually passing arrays to the function and not strings containing lists? Because that's what the single-dimensional variant expects (which is weird), so I guess you've simply stumbled upon your own coding and messed up the test values.

    With arrays, you should get a correct result.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    199
    Rep Power
    29
    Originally Posted by Jacques1
    Hi,

    are you sure you're actually passing arrays to the function and not strings containing lists? Because that's what the single-dimensional variant expects (which is weird), so I guess you've simply stumbled upon your own coding and messed up the test values.

    With arrays, you should get a correct result.
    .......Wow.... How did I not see that. Thank you.


    Just in cast anybody comes along looking to do something similar... this is what I did to fix it:

    I moved this part up to the top of the function to generate the array from the comma separated list.
    PHP Code:
            $keyarray=array_map('trim'explode(","$keylist)); 
            
    $valuesarray=array_map('trim'explode(","$valueslist)); 
    PHP Code:
        function dataArrayCombine($valueslist$keylist$multi=false){
        
    $keyarray=array_map('trim'explode(","$keylist));
        
    $valuesarray=array_map('trim'explode(","$valueslist));
            if(
    $multi===true){
            
    $combinedarray=array();
                foreach(
    $keyarray as $ia => $ka) {
                    
    $combinedarray[$ka][]=$valuesarray[$ia];
                }
            
    array_walk($combinedarraycreate_function('&$v''$v=(count($v)==1) ? array_pop($v) : $v;'));
            }
            else{
            
    $combinedarray=array_combine($keyarray$valuesarray);
            }
        return 
    $combinedarray;
        } 
    Last edited by HDFilmMaker2112; August 17th, 2013 at 03:03 PM.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    I don't think this is a good fix.

    The first problem is that your function doesn't do what it says. It's called dataArrayCombine(), but it does not combine two arrays. Instead, it expects two CSV strings -- which is a bad way of implementing lists, by the way. The second problem is that it's too specialized. You can't use it for anything but your special CSV strings. If you simply wanna combine two ordinary arrays, you can't do that (unless you turn them into strings first).

    Confusing code is bad code, because it's almost guaranteed to cause misunderstandings and bugs. This is obvious if you're working in a team. But even if you're doing this just for fun and you're the only one who'll ever work on the code, it's still important to do it properly. Because after a year or maybe even a month, you won't remember all quirks, pitfalls and tricks. And then your dataArrayCombine() function is probably the first thing you'll stumble upon.

    Rewrite the function so that it takes two arrays. Or at least rename it.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo