我的页面中有这个小搜索功能; $ searchWord是我需要在我的数组中查找的单词。 该数组看起来有点像这样:
stdClass Object (
[ActionScopeId] => 181365
[DateChanged] => 0001-01-01T00:00:00
[DateCreated] => 2013-08-20T13:59:33.053
[Description] => Snelheid test
[MessageCode] => C0000448220
)
stdClass Object (
[ActionScopeId] => 181364
[DateChanged] => 0001-01-01T00:00:00
[DateCreated] => 2013-08-14T10:08:50.707
[Description] => Test
[MessageCode] => C0000448219
)
现在,例如;我想找“测试”这个词。当找到它时,我想用它打印ActionScopeId和DateCreated。
这是我的代码:
$roc = array('relation' => $_SESSION['username']);
$rocresponse = $wcfclient->ReadOpenCalls($roc);
foreach ($rocresponse->ReadOpenCallsResult as $key => $value){
foreach ($value as $key1 => $value1){
if (array_search($searchWord,$value1)){
echo $value1->ActionScopeId;
}
}
}
但是,我得到的结果总是空的。我做错了什么?
我修复了它,在尝试搜索对象时,您可以使用此功能:
function in_object($val, $obj){
if($val == ""){
trigger_error("in_object expects parameter 1 must not empty", E_USER_WARNING);
return false;
}
if(!is_object($obj)){
$obj = (object)$obj;
}
foreach($obj as $key => $value){
if(!is_object($value) && !is_array($value)){
if($value == $val){
return true;
}
}else{
return in_object($val, $value);
}
}
return false;
}
然后以这种方式寻找它:
if (in_object($searchWord,$value)){
答案 0 :(得分:0)
我通过使用此功能解决了这个问题:
function in_object($val, $obj){
if($val == ""){
trigger_error("in_object expects parameter 1 must not empty", E_USER_WARNING);
return false;
}
if(!is_object($obj)){
$obj = (object)$obj;
}
foreach($obj as $key => $value){
if(!is_object($value) && !is_array($value)){
if($value == $val){
return true;
}
}else{
return in_object($val, $value);
}
}
return false;
然后以这种方式寻找它:
if (in_object($searchWord,$value)){}