1

I have a multi-dimensional array and have tried every example code I can find from this website to sort it by a column. Not a single snippet I have tried has worked and for some reason all of them result in some strange mess of ordering. I cannot for the life of me figure out what is causing this and hope someone can point it out...

    if ($devices_xml = curl_get_file_contents($devices_url))
    {
        $devices = simplexml_load_string($devices_xml);

        $data = array(array());
        $counter = 0;

        foreach ($devices->item as $device)
        {
            $data[$counter]["id"] = $device->objid;
            $data[$counter]["probe"] = $device->probe;
            $data[$counter]["name"] = $device->device;

            $counter++;
        }
        
        array_sort_by_column($data, "probe");
        return $data;
    }
    
    return false;
}

My Multi-dimensional sorting function that works for everything else but not this is as follows...

function array_sort_by_column(&$arr, $col, $dir = SORT_ASC)
{
    $sort_col = array();
    foreach ($arr as $key=> $row)
    {
        $sort_col[$key] = $row[$col];
    }

    array_multisort($sort_col, $dir, $arr);
}

The result comes out looking like this. Probe is the test such as "DE-FRANKFURT" at the beginning and name is the second part such as "EU-DE-010"

enter image description here

0

1 Answer 1

2

This was fixed by casting the SimpleXML Objects to strings. None of the sorting functions were working correctly until that was done, then it was correct.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.