PHP |在阵列搜索 [英] php | Search in array

查看:98
本文介绍了PHP |在阵列搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,想找到最大的评级对象吗?我的数组类似如下:

 阵列

    [0] => stdClass的对象
        (
            [CREATED_BY] => 905
            [评分] => 1
        )
    [1] => stdClass的对象
        (
            [CREATED_BY] => 906
            [评分] => 2
        )

谢谢!

我已经试过这样的事情:

 静态函数maxValueInArray($数组$ keyToSearch){
        $ currentMax = NULL;
        的foreach($数组作为编曲$){
            的foreach($改编为$关键=> $值){
                如果($键== $ keyToSearch&放大器;及($值> = $ currentMax)){
                    $ currentMax = $价值;
                }
            }
        }        返回$ currentMax;
    }


解决方案

由于你的数组没有排序(我假设),你需要阅读所有值。

  $ maxobject = NULL;
的foreach($数组作为$行)
  如果(is_null($ maxobject)|| $ maxobject->评级< $行向>评级)
    $ maxobject = $行;

I have an array and would like to find the object with maximal rating? My array looks like below:

Array
(
    [0] => stdClass Object
        (
            [created_by] => 905
            [rating] => 1
        )
    [1] => stdClass Object
        (
            [created_by] => 906
            [rating] => 2
        )
)

Thanks!

I have tried something like that:

static function maxValueInArray($array, $keyToSearch) {
        $currentMax = NULL;
        foreach($array as $arr) {
            foreach($arr as $key => $value) {
                if ($key == $keyToSearch && ($value >= $currentMax)) {
                    $currentMax = $value;
                }
            }
        }

        return $currentMax;
    }

解决方案

Since your array is not sorted (I assume), you will need to read all values.

$maxobject = null;
foreach($array as $row)
  if(is_null($maxobject) || $maxobject->rating<$row->rating)
    $maxobject=$row;

这篇关于PHP |在阵列搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆