简体   繁体   English

按列对多维数组排序

[英]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" 探针开头是测试,例如“ DE-FRANKFURT”,名称是第二部分,例如“ EU-DE-010”

在此处输入图片说明

This was fixed by casting the SimpleXML Objects to strings. 通过将SimpleXML对象转换为字符串可以解决此问题。 None of the sorting functions were working correctly until that was done, then it was correct. 在完成排序功能之前,所有排序功能都无法正常工作,然后才是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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