0

我的页面中有这个小搜索功能;$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)){
4

1 回答 1

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)){}
于 2013-08-29T15:06:38.523 回答