Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中通过对象数组中的属性进行搜索?_Php_Arrays - Fatal编程技术网

如何在php中通过对象数组中的属性进行搜索?

如何在php中通过对象数组中的属性进行搜索?,php,arrays,Php,Arrays,我有一个数组,如下所示 如果我在php中提供slab id值,是否可以找到数组的键索引 Array ( [0] => incentiveSlab Object ( [slabId] => 1 [templateId] => 1 [startPoint] => 0

我有一个数组,如下所示 如果我在php中提供slab id值,是否可以找到数组的键索引

Array
       (
           [0] => incentiveSlab Object
               (
                   [slabId] => 1
                   [templateId] => 1
                   [startPoint] => 0
                   [endPoint] => 1000000
                   [value] => 0
               )

           [1] => incentiveSlab Object
               (
                   [slabId] => 2
                   [templateId] => 1
                   [startPoint] => 1000000
                   [endPoint] => 2500000
                   [value] => 0.5
               )


       )
诸如此类:

function getIndex($array, $slabId) {
    foreach($array as $index => $item) {
        if($item->slabId == $slabId)
            return $index;
    }
}
诸如此类:

function getIndex($array, $slabId) {
    foreach($array as $index => $item) {
        if($item->slabId == $slabId)
            return $index;
    }
}

如果找到与上述答案类似的索引,则解决方案应返回该索引。但是,如果不匹配,它应该返回其他内容,就像其他标准函数一样,如果不匹配,它将返回
false
。因此,
Max
答案可以稍微修改为:

function getIndex($array, $slabId) {
    foreach($array as $index => $item) {
        if($item->slabId == $slabId)
            return $index;
    }
    return false; // or return -1
}

如果找到与上述答案类似的索引,则解决方案应返回该索引。但是,如果不匹配,它应该返回其他内容,就像其他标准函数一样,如果不匹配,它将返回
false
。因此,
Max
答案可以稍微修改为:

function getIndex($array, $slabId) {
    foreach($array as $index => $item) {
        if($item->slabId == $slabId)
            return $index;
    }
    return false; // or return -1
}
我建议,改变你的数据结构

  • 使用slabId作为数组索引
  • 例如

    排列

    (

    )

或者,如果斯拉夫语中的差异太大,请使用关联数组

我建议,改变你的数据结构

  • 使用slabId作为数组索引
  • 例如

    排列

    (

    )

或者,如果斯拉夫语中的差异太大,请使用关联数组


我在找内置函数我在找内置函数