简体   繁体   中英

Sort Multidimensional Array by Column

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"

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM