Search code examples
phparrayssortingmultidimensional-arraymerging-data

Isolate values from multiple columns of a 2d array then sort the values


The sorting of array in alphabetical order is not outputting correctly.

It outputted as:

Demo
qwerty
Demo3
Test1
Test2
New1

Instead of:

Demo
Demo3
New1
qwerty
Test1
Test2

Code:

$dbresults = array(
    "0" => array (
        "id" => "1",
        "cb_boutiquename1" => "Test1",
        "cb_boutiquename2" => "Test2",
        "cb_boutiquename3" => "New1"
    ),
    "1" => array (
        "id" => "2",
        "cb_boutiquename1" => "Demo",
        "cb_boutiquename2" => "qwerty",
        "cb_boutiquename3" => "Demo3"
    )
);
           
function sortarray($a, $b) {
    return strcmp($a["cb_boutiquename$i"], $b["cb_boutiquename$i"]);
    }

    usort($dbresults, "sortarray");

    while (list($key, $value) = each($dbresults)) {
        $results[] = $value ;
    }
    
    foreach($results as $result) {
        $i = 1;
        while (array_key_exists("cb_boutiquename$i", $result)) {
        if ($result["cb_boutiquename$i"] !='') {
            echo '<a href=' . cbSef( 'index.php?option=com_comprofiler&task=page&user=' . (int) $result['id'] . '&b=' . $i . getCBprofileItemid( false )) . '>' . $result["cb_boutiquename$i"] . '</a><br />';
        }
        ++$i;
    }
}

Solution

  • Your problem is that in here:

    function sortarray($a, $b) {
        return strcmp($a["cb_boutiquename$i"], $b["cb_boutiquename$i"]);
    }
    

    $i is undefined so you are basically comparing one undefined array offset with another.

    If you want to specify which of the cb_boutiquename fields to specify at runtime then what you probably want to do is include the $i in the function, on php this can be done with a function that returns a closure (PHP 5.3+).

    function getSorter($idx){
           return function($a, $b) use ($idx) {
               return strcmp($a["cb_boutiquename$idx"], $b["cb_boutiquename$idx"]);
           };
    }
    

    This returns a function that closes over the supplied argument and is appropriate for use as a comparison function for usort. So for your sort call you would use:

    usort($dbresults, getSorter(1));
    

    Or for a more general solution that does not assume the cb_boutiquename prefix on the properties you can change this to

    function getSorter($idx){
           return function($a, $b) use ($idx) {
               return strcmp($a[$idx], $b[$idx]);
           };
    }
    usort($dbresults, getSorter("cb_boutiquename1"));
    

    This will allow you to sort any array of arrays by an one of the indexes.

    UPDATE I completely misunderstood the goal of this exercise. What you want to do is to flatten your array prior to doing any sorting.

        $dbresults= array ( "0"  => array ( "id" => "1",
                              "cb_boutiquename1" => "Test1",
                              "cb_boutiquename2" => "Test2",
                              "cb_boutiquename3" => "New1"
                                 ),
              "1" => array ( "id" => "2",
                              "cb_boutiquename1" => "Demo",
                              "cb_boutiquename2" => "qwerty",
                              "cb_boutiquename3" => "Demo3"
                                 )
            );
    
    // flatten out the array
    $results = array();
    foreach($dbresults as $k=>$v){
        foreach ($v as $key=>$value) {
              if (substr($key, 0,15) == "cb_boutiquename"){
                  $results[] = array("id"=>$v["id"], "cb_boutiquename"=>$value, "i"=>substr($key, 15));
              }
        }
    
    }
    
    usort($results, function($a, $b){ return strcasecmp($a["cb_boutiquename"], $b["cb_boutiquename"]); });
    foreach($results as $result){
        echo '<a href=' . cbSef( 'index.php?option=com_comprofiler&task=page&user=' . (int) $result['id'] . '&b=' . $result["i"] . getCBprofileItemid( false )). '>' . $result["cb_boutiquename"] . '</a><br />'."\n";
    }