尝试将 usort 与 strcmp 一起使用以按 属性 对对象进行排序
Trying to use usort with strcmp to sort objects by property
看着 this post 它似乎很简单,但我无法让它工作。
我构建了一个这样的对象数组:
objs = array();
loop..
$obj = array(
"prop1" => 'cccc a a a a 32423f sdf',
"prop2" => 'sdlkfjsldkfj',
"prop3" => 'slkfjs93s3jfsl'
);
$Objs[] = $obj2;
然后我尝试在 属性 上按字母顺序对对象进行排序:
function cmp($a, $b) {
return strcasecmp($a->prop1, $b->prop1);
}
usort($fileObjs, "cmp");
但它没有按字母顺序对它们进行排序,我不确定我哪里出错了。这是一个完整的例子:
$objs = array();
$obj1 = array(
"prop1" => 'aaaa a a a a 32423f sdf',
"prop2" => 'sdlkfjsldkfj',
"prop3" => 'slkfjs93s3jfsl'
);
$obj2 = array(
"prop1" => 'bbb a a a a 32423f sdf',
"prop2" => 'sdlkfjsldkfj',
"prop3" => 'slkfjs93s3jfsl'
);
$obj3 = array(
"prop1" => 'cccc a a a a 32423f sdf',
"prop2" => 'sdlkfjsldkfj',
"prop3" => 'slkfjs93s3jfsl'
);
$objs[] = $obj2;
$objs[] = $obj1;
$objs[] = $obj3;
function cmp($a, $b) {
return strcasecmp($a->prop1, $b->prop1);
}
usort($objs, "cmp");
var_dump($objs);
结果输出:
array(3) {
[0]=>
array(3) {
["prop1"]=>
string(23) "cccc a a a a 32423f sdf"
["prop2"]=>
string(12) "sdlkfjsldkfj"
["prop3"]=>
string(14) "slkfjs93s3jfsl"
}
[1]=>
array(3) {
["prop1"]=>
string(23) "aaaa a a a a 32423f sdf"
["prop2"]=>
string(12) "sdlkfjsldkfj"
["prop3"]=>
string(14) "slkfjs93s3jfsl"
}
[2]=>
array(3) {
["prop1"]=>
string(22) "bbb a a a a 32423f sdf"
["prop2"]=>
string(12) "sdlkfjsldkfj"
["prop3"]=>
string(14) "slkfjs93s3jfsl"
}
}
看来你混淆了对象和数组对象。您的 cmp 函数正在比较您的数组对象中不存在的属性。
如果你:
echo "comparing {$a->prop1} vs {$b->prop1}";
在 cmp 方法中,您会注意到 prop1 为空。
您可以像这样引用数组索引来修复您的代码
function cmp($a, $b) {
return strcasecmp($a['prop1'], $b['prop1']);
}
看着 this post 它似乎很简单,但我无法让它工作。
我构建了一个这样的对象数组:
objs = array();
loop..
$obj = array(
"prop1" => 'cccc a a a a 32423f sdf',
"prop2" => 'sdlkfjsldkfj',
"prop3" => 'slkfjs93s3jfsl'
);
$Objs[] = $obj2;
然后我尝试在 属性 上按字母顺序对对象进行排序:
function cmp($a, $b) {
return strcasecmp($a->prop1, $b->prop1);
}
usort($fileObjs, "cmp");
但它没有按字母顺序对它们进行排序,我不确定我哪里出错了。这是一个完整的例子:
$objs = array();
$obj1 = array(
"prop1" => 'aaaa a a a a 32423f sdf',
"prop2" => 'sdlkfjsldkfj',
"prop3" => 'slkfjs93s3jfsl'
);
$obj2 = array(
"prop1" => 'bbb a a a a 32423f sdf',
"prop2" => 'sdlkfjsldkfj',
"prop3" => 'slkfjs93s3jfsl'
);
$obj3 = array(
"prop1" => 'cccc a a a a 32423f sdf',
"prop2" => 'sdlkfjsldkfj',
"prop3" => 'slkfjs93s3jfsl'
);
$objs[] = $obj2;
$objs[] = $obj1;
$objs[] = $obj3;
function cmp($a, $b) {
return strcasecmp($a->prop1, $b->prop1);
}
usort($objs, "cmp");
var_dump($objs);
结果输出:
array(3) {
[0]=>
array(3) {
["prop1"]=>
string(23) "cccc a a a a 32423f sdf"
["prop2"]=>
string(12) "sdlkfjsldkfj"
["prop3"]=>
string(14) "slkfjs93s3jfsl"
}
[1]=>
array(3) {
["prop1"]=>
string(23) "aaaa a a a a 32423f sdf"
["prop2"]=>
string(12) "sdlkfjsldkfj"
["prop3"]=>
string(14) "slkfjs93s3jfsl"
}
[2]=>
array(3) {
["prop1"]=>
string(22) "bbb a a a a 32423f sdf"
["prop2"]=>
string(12) "sdlkfjsldkfj"
["prop3"]=>
string(14) "slkfjs93s3jfsl"
}
}
看来你混淆了对象和数组对象。您的 cmp 函数正在比较您的数组对象中不存在的属性。
如果你:
echo "comparing {$a->prop1} vs {$b->prop1}";
在 cmp 方法中,您会注意到 prop1 为空。
您可以像这样引用数组索引来修复您的代码
function cmp($a, $b) {
return strcasecmp($a['prop1'], $b['prop1']);
}