2014-01-16 70 views
0

PHP對象我有一個PHP對象調用$result,我可以用看的價值:如何搜索文本

print_r($result); 

我想搜索$result爲整個對象特定文本就像如果我看的print_r($result)結果,做了「發現」通過Web瀏覽器。

不幸的是我很難做到這一點 - 我知道isset可以搜索屬性,但我正在尋找更多的屬性,因爲我正在尋找文本。我試圖運行以下:

if (strpos($result,'[test] => lots of text to search') !== false) { 
echo 'true'; 
} 

但不幸的是沒有回來(沒有錯誤和沒有響應)。任何建議如何搜索這個對象將不勝感激!

回答

1

我想你想要做的

if (isset($result->test) && $result->test == "lots of text to search") { 
    echo 'true'; 
} 

,但如果你想真正文本搜索結果,嘗試

if (strpos(print_r($result, true),'[test] => lots of text to search') !== false) { 
    echo 'true'; 
} 
+0

我認爲提問者一定要學會如何正確使用對象,而不是搜索序列化/傾銷對象的值。強調第一個代碼段絕對是正確的做法。 – Scopey

+0

謝謝!這完全適用於我! – AAA

0

如果你的對象是一個數組,你可以嘗試使用array_search()

下面是一個例子:

<?php 
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red'); 

$key = array_search('green', $array); // $key = 2; 
$key = array_search('red', $array); // $key = 1; 
?>