如何在php中通过对象数组中的属性进行搜索?
我有一个数组,如下所示 如果我在php中提供slab id值,是否可以找到数组的键索引如何在php中通过对象数组中的属性进行搜索?,php,arrays,Php,Arrays,我有一个数组,如下所示 如果我在php中提供slab id值,是否可以找到数组的键索引 Array ( [0] => incentiveSlab Object ( [slabId] => 1 [templateId] => 1 [startPoint] => 0
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作为数组索引
- 例如 排列 ( )
我在找内置函数我在找内置函数