本文介绍了按值对php多维数组排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按变量"i_lastname"对多维php对象进行排序.

I need to sort this multidimensional php object by the variable 'i_lastname'.

我用usort和ksort尝试了大多数示例,但没有成功.大多数示例都与此类似,但对我不起作用.

I tried most examples with usort and ksort to no avail. Most examples are like this one but won't work for me.

function sort_by_name($a, $b)
{
  return ($a->i_lastname < $b->i_lastname ? -1 : 1);
}
usort($cards, 'sort_by_name');

这是对象结构.

谢谢.

Array
(
    [0] => Array
        (
            [officer_id] => 4D5DEF60
            [card_id] => 5
            [general] => Array
                (
                    [0] => Array
                        (
                            [i_officer_id] => 4D5DEF60
                            [i_officer_name] =>
                            [i_datetime] => 2012-06-12 13:47:00
                            [i_location] => 17 Rue de Neuch, Yvette, France
                            [i_lat] => 48.6955
                            [i_lon] => 2.21458
                            [i_reason] => DUI
                        )
                )
            [subject] => Array
                (
                    [0] => Array
                        (
                            [subject_id] => 6
                            [i_lastname] => Toro
                            [i_firstname] => Benicio
                        )
                    [1] => Array
                        (
                            [subject_id] => 9
                            [i_lastname] => Limon
                            [i_firstname] => Pedro
                        )
                )
            [vehicle] => Array
                (
                )
        )

    [1] => Array
        (
            [officer_id] => 4D5DEF60
            [card_id] => 2
            [general] => Array
                (
                    [0] => Array
                        (
                            [i_officer_id] => 0f3b45a3
                            [i_officer_name] =>
                            [i_datetime] => 2012-06-06 19:42:00
                            [i_location] => 10231 Yankee St, Miami, USA
                            [i_lat] => 39.5952
                            [i_lon] => -84.2003
                            [i_reason] => Speeding
                        )
                )
            [subject] => Array
                (
                    [0] => Array
                        (
                            [subject_id] => 3
                            [i_lastname] => Doe
                            [i_firstname] => John
                        )
                    [0] => Array
                        (
                            [subject_id] => 15
                            [i_lastname] => Doe
                            [i_firstname] => Jane
                        )
                )
            [vehicle] => Array
                (
                )
        )
)

推荐答案

您应该使用['...']而不是->...-您正在处理数组,而不是对象.

You should use ['...'] rather than ->... - you're dealing with arrays, not objects.

您的回调应如下所示:

return strnatcasecmp($a['i_lastname'],$b['i_lastname']);

任何 strcmp strcasecmp strnatcmp strnatcasecmp 应该可以正常工作,您只需要确定哪个最适合您的工作即可.

Any of strcmp, strcasecmp, strnatcmp or strnatcasecmp should work fine, you just need to decide which one works best for what you are doing.

这篇关于按值对php多维数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:05