获取二维对象数组中具有 min() 列值的对象

发布于 2024-10-20 11:00:46 字数 812 浏览 6 评论 0原文

我正在尝试从对象数组中检索距离值最小的对象。这是我的数据集如下;

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

心房敞 2024-10-27 11:00:46

嗯,对于 PHP >= 5.3 我会尝试这样的事情:

$Object = array_reduce($data,function($A,$B){
    return $A->distance < $B->distance ? $A : $B;
})

对于 PHP <= 5.3 5.3 填充就足够了:

function user_defined_reduce($A,$B){
    return $A->distance < $B->distance ? $A : $B;
}
$Object = array_reduce($data,"user_defined_reduce");

Hmm for PHP >= 5.3 I would try something like this:

$Object = array_reduce($data,function($A,$B){
    return $A->distance < $B->distance ? $A : $B;
})

For PHP < 5.3 the fillowing would suffice:

function user_defined_reduce($A,$B){
    return $A->distance < $B->distance ? $A : $B;
}
$Object = array_reduce($data,"user_defined_reduce");
梦初启 2024-10-27 11:00:46

之前建议的答案没有解释需要在 array_reduce() 中包含 $initial 值。因此该解决方案不起作用。

这个对我有用(PHP 5.3.13):

$array = array(
    array(
        'name' => 'something',
        'value' => 56
    ),
    array(
        'name' => 'else',
        'value' => 54
    ),
    array(
        'name' => 'else',
        'value' => 58
    ),
    array(
        'name' => 'else',
        'value' => 78
    )
);
$object = array_reduce($array, function($a, $b){
    return $a['value'] < $b['value'] ? $a : $b;
}, array_shift($array));

print_r($object);

这会给我:

[0] => Array
(
    [name] => else
    [value] => 54
)

而之前的解决方案给了我null。我假设 PHP < 5.3 需要为 array_reduce() 指定类似的初始值。

Previously suggested answers did not explain the need to include an $initial value to array_reduce(). The solution did not work because of it.

This one works for me (PHP 5.3.13):

$array = array(
    array(
        'name' => 'something',
        'value' => 56
    ),
    array(
        'name' => 'else',
        'value' => 54
    ),
    array(
        'name' => 'else',
        'value' => 58
    ),
    array(
        'name' => 'else',
        'value' => 78
    )
);
$object = array_reduce($array, function($a, $b){
    return $a['value'] < $b['value'] ? $a : $b;
}, array_shift($array));

print_r($object);

This will give me:

[0] => Array
(
    [name] => else
    [value] => 54
)

Whereas the previous solution gave me null. I'm assuming that PHP < 5.3 would require a similar initial value to be specified to array_reduce().

旧人哭 2024-10-27 11:00:46

您无法将其展平,因为它不是一个仅包含值的普通旧多维数组。

这应该有效:

$min = $object[0];
for ($i = 1; $i < count($object); $i++)
    if ($object[$i]['distance'] < $min['distance'])
        $min = $object[$i];

You can't flatten this as it isn't a plain old multi-dimensional array with just the values.

This should work:

$min = $object[0];
for ($i = 1; $i < count($object); $i++)
    if ($object[$i]['distance'] < $min['distance'])
        $min = $object[$i];
萌面超妹 2024-10-27 11:00:46

这个例子应该给你一个正确方向的提示:

<?php

$a = new stdClass();
$a->foo = 2;
$b = new stdClass();
$b->foo = 3;
$c = new stdClass();
$c->foo = 1;

$init = new stdClass();
$init->foo = 1000;

$vals = array( $a, $b, $c );

var_dump(
    array_reduce(
        $vals,
        function ( $x, $y )
        {
            if ( $x->foo < $y->foo )
            {
                return $x;
            }   
            else
            {
                return $y;
            }   
        },  
        $init
    )   
);  

?>

This example should give you a hint in the right direction:

<?php

$a = new stdClass();
$a->foo = 2;
$b = new stdClass();
$b->foo = 3;
$c = new stdClass();
$c->foo = 1;

$init = new stdClass();
$init->foo = 1000;

$vals = array( $a, $b, $c );

var_dump(
    array_reduce(
        $vals,
        function ( $x, $y )
        {
            if ( $x->foo < $y->foo )
            {
                return $x;
            }   
            else
            {
                return $y;
            }   
        },  
        $init
    )   
);  

?>
爱情眠于流年 2024-10-27 11:00:46

尝试一下:

$myObjectCollection = ...;
$minObject = $myObjectCollection[0]; // or reset($myObjectCollection) if you can't ensure  numeric 0 index

array_walk($myObjectCollection, function($object) use ($minObject) { 
        $minObject = $object->distance < $minObject->distance ? $object : $minObject;
});

但是从你的转储的外观来看。 namedistance 是私有的。因此,您希望能够使用 -> 直接从对象访问它们。你需要某种 getter getDistance()

try:

$myObjectCollection = ...;
$minObject = $myObjectCollection[0]; // or reset($myObjectCollection) if you can't ensure  numeric 0 index

array_walk($myObjectCollection, function($object) use ($minObject) { 
        $minObject = $object->distance < $minObject->distance ? $object : $minObject;
});

but from the looks of your dump. name and distance are private. So you want be able to access them from the object directly using ->. You'll need some kind of getter getDistance()

音栖息无 2024-10-27 11:00:46
$objectsArray = array(...); // your objects
$distance     = null;
$matchedObj   = null;

foreach ( $objectsArray as $obj ) {
   if ( is_null($distance) || ( $obj->distance < $distance ) ) {
      $distance   = $obj->distance;
      $matchedObj = $obj;
   }
}

var_dump($matchedObj);

AD 编辑:

如果您要使用数组而不是对象,请将 $obj->distance 更改为 $obj['distance']

$objectsArray = array(...); // your objects
$distance     = null;
$matchedObj   = null;

foreach ( $objectsArray as $obj ) {
   if ( is_null($distance) || ( $obj->distance < $distance ) ) {
      $distance   = $obj->distance;
      $matchedObj = $obj;
   }
}

var_dump($matchedObj);

AD EDIT:

If you will use arrays instead of objects, change $obj->distance to $obj['distance'].

小矜持 2024-10-27 11:00:46

当我寻找一种找到最低值而不是整个对象的方法时,我偶然发现了这个问题。如果其他人正在寻找同样的东西,这是我的方法:

$min_value = min( array_map( fn($o) => $o->my_prop, $objects ) );

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:

$min_value = min( array_map( fn($o) => $o->my_prop, $objects ) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文