获取二维对象数组中具有 min() 列值的对象
我正在尝试从对象数组中检索距离值最小的对象。这是我的数据集如下;
[0] => myObjectThing Object
(
[name:myObjectThing:private] => asadasd
[distance:myObjectThinge:private] => 0.9826368952306
)
[1] => myObjectThing Object
(
[name:myObjectThing:private] => 214gerwert24
[distance:myObjectThinge:private] => 1.5212312547306
)
[2] => myObjectThing Object
(
[name:myObjectThing:private] => abc123
[distance:myObjectThinge:private] => 0.0000368952306
)
所以我希望能够检索具有最小距离值的对象。在这种情况下,它将是名称为:abc123
**编辑**如果我只使用数组会发生什么,例如,
array(
array('name' => 'bla', 'distance' => '123');
array('name' => 'b123a', 'distance' => '1234214');
);
这会更容易找到最小值吗?
I'm trying to retrieve the object which has the lowest distance value from an array of objects. This is my data set below;
[0] => myObjectThing Object
(
[name:myObjectThing:private] => asadasd
[distance:myObjectThinge:private] => 0.9826368952306
)
[1] => myObjectThing Object
(
[name:myObjectThing:private] => 214gerwert24
[distance:myObjectThinge:private] => 1.5212312547306
)
[2] => myObjectThing Object
(
[name:myObjectThing:private] => abc123
[distance:myObjectThinge:private] => 0.0000368952306
)
So I'd like to be able to retieve the object which has the smallest distance value. In this case it would be object with name: abc123
** EDIT ** What would happen if I only used arrays, e.g.
array(
array('name' => 'bla', 'distance' => '123');
array('name' => 'b123a', 'distance' => '1234214');
);
Would this be easier to find the min value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
嗯,对于 PHP >= 5.3 我会尝试这样的事情:
对于 PHP <= 5.3 5.3 填充就足够了:
Hmm for PHP >= 5.3 I would try something like this:
For PHP < 5.3 the fillowing would suffice:
之前建议的答案没有解释需要在
array_reduce()
中包含$initial
值。因此该解决方案不起作用。这个对我有用(PHP 5.3.13):
这会给我:
而之前的解决方案给了我
null
。我假设 PHP < 5.3 需要为 array_reduce() 指定类似的初始值。Previously suggested answers did not explain the need to include an
$initial
value toarray_reduce()
. The solution did not work because of it.This one works for me (PHP 5.3.13):
This will give me:
Whereas the previous solution gave me
null
. I'm assuming that PHP < 5.3 would require a similar initial value to be specified toarray_reduce()
.您无法将其展平,因为它不是一个仅包含值的普通旧多维数组。
这应该有效:
You can't flatten this as it isn't a plain old multi-dimensional array with just the values.
This should work:
这个例子应该给你一个正确方向的提示:
This example should give you a hint in the right direction:
尝试一下:
但是从你的转储的外观来看。
name
和distance
是私有的。因此,您希望能够使用->
直接从对象访问它们。你需要某种 gettergetDistance()
try:
but from the looks of your dump.
name
anddistance
are private. So you want be able to access them from the object directly using->
. You'll need some kind of gettergetDistance()
AD 编辑:
如果您要使用数组而不是对象,请将
$obj->distance
更改为$obj['distance']
。AD EDIT:
If you will use arrays instead of objects, change
$obj->distance
to$obj['distance']
.当我寻找一种找到最低值而不是整个对象的方法时,我偶然发现了这个问题。如果其他人正在寻找同样的东西,这是我的方法:
I stumbled on this question while I was searching for a way to find the lowest value instead of the entire object. In case somebody else is looking for the same thing, here's my approach: