2014-11-18 1 views
1

Я пытаюсь выполнить итерацию объекта ниже, используя foreach в PHP, но я не получаю никакого результата.Почему isset и не пустой не работает в этом объекте Solarium iteration

Solarium\QueryType\Select\Result\Document Object 
(
    [fields:protected] => Array 
     (
      [id] => 3007WFP 
      [name] => Dell Widescreen UltraSharp 3007WFP 
      [manu] => Dell, Inc. 
      [manu_id_s] => dell 
      [cat] => Array 
       (
        [0] => electronics and computer1 
       ) 

      [features] => Array 
       (
        [0] => 30" TFT active matrix LCD, 2560 x 1600, .25mm dot pitch, 700:1 contrast 
       ) 

      [includes] => USB cable 
      [weight] => 401.6 
      [price] => 2199 
      [price_c] => 2199,USD 
      [popularity] => 6 
      [inStock] => 1 
      [store] => 43.17614,-90.57341 
      [_version_] => 1.485082319029E+18 
      [score] => 1 
     ) 

) 

Solarium\QueryType\Select\Result\Document Object 
(
    [fields:protected] => Array 
     (
      [id] => book2 
      [cat] => Array 
       (
        [0] => fantasy 
       ) 

      [pubyear_i] => 1996 
      [title] => Array 
       (
        [0] => A Game of Thrones 
       ) 

      [author] => George R.R. Martin 
      [author_s] => George R.R. Martin 
      [series_s] => A Song of Ice and Fire 
      [sequence_i] => 1 
      [_version_] => 1.4850828331521E+18 
      [score] => 1 
     ) 

) 

Solarium\QueryType\Select\Result\Document Object 
(
    [fields:protected] => Array 
     (
      [id] => book3 
      [cat] => Array 
       (
        [0] => fantasy 
       ) 

      [pubyear_i] => 1999 
      [title] => Array 
       (
        [0] => A Clash of Kings 
       ) 

      [author] => George R.R. Martin 
      [author_s] => George R.R. Martin 
      [series_s] => A Song of Ice and Fire 
      [sequence_i] => 2 
      [_version_] => 1.4850828331574E+18 
      [score] => 1 
     ) 

) 

Solarium\QueryType\Select\Result\Document Object 
(
    [fields:protected] => Array 
     (
      [id] => book4 
      [cat] => Array 
       (
        [0] => sci-fi 
       ) 

      [pubyear_i] => 1951 
      [title] => Array 
       (
        [0] => Foundation 
       ) 

      [author] => Isaac Asimov 
      [author_s] => Isaac Asimov 
      [series_s] => Foundation Series 
      [sequence_i] => 1 
      [_version_] => 1.4850828331584E+18 
      [score] => 1 
     ) 

) 

Solarium\QueryType\Select\Result\Document Object 
(
    [fields:protected] => Array 
     (
      [id] => book5 
      [cat] => Array 
       (
        [0] => sci-fi 
       ) 

      [pubyear_i] => 1952 
      [title] => Array 
       (
        [0] => Foundation and Empire 
       ) 

      [author] => Isaac Asimov 
      [author_s] => Isaac Asimov 
      [series_s] => Foundation Series 
      [sequence_i] => 2 
      [_version_] => 1.4850828331605E+18 
      [score] => 1 
     ) 

) 

Solarium\QueryType\Select\Result\Document Object 
(
    [fields:protected] => Array 
     (
      [id] => book6 
      [cat] => Array 
       (
        [0] => sci-fi 
       ) 

      [pubyear_i] => 1992 
      [title] => Array 
       (
        [0] => Snow Crash 
       ) 

      [author] => Neal Stephenson 
      [author_s] => Neal Stephenson 
      [series_s] => Snow Crash 
      [_version_] => 1.4850828331616E+18 
      [score] => 1 
     ) 

) 

Solarium\QueryType\Select\Result\Document Object 
(
    [fields:protected] => Array 
     (
      [id] => book7 
      [cat] => Array 
       (
        [0] => sci-fi 
       ) 

      [pubyear_i] => 1984 
      [title] => Array 
       (
        [0] => Neuromancer 
       ) 

      [author] => William Gibson 
      [author_s] => William Gibson 
      [series_s] => Sprawl trilogy 
      [sequence_i] => 1 
      [_version_] => 1.4850828331636E+18 
      [score] => 1 
     ) 

) 

Solarium\QueryType\Select\Result\Document Object 
(
    [fields:protected] => Array 
     (
      [id] => book8 
      [cat] => Array 
       (
        [0] => fantasy 
       ) 

      [pubyear_i] => 1985 
      [title] => Array 
       (
        [0] => The Black Company 
       ) 

      [author] => Glen Cook 
      [author_s] => Glen Cook 
      [series_s] => The Black Company 
      [sequence_i] => 1 
      [_version_] => 1.4850828331647E+18 
      [score] => 1 
     ) 

) 

Solarium\QueryType\Select\Result\Document Object 
(
    [fields:protected] => Array 
     (
      [id] => book9 
      [cat] => Array 
       (
        [0] => fantasy 
       ) 

      [pubyear_i] => 1965 
      [title] => Array 
       (
        [0] => The Black Cauldron 
       ) 

      [author] => Lloyd Alexander 
      [author_s] => Lloyd Alexander 
      [series_s] => The Chronicles of Prydain 
      [sequence_i] => 2 
      [_version_] => 1.4850828331657E+18 
      [score] => 1 
     ) 

) 

Solarium\QueryType\Select\Result\Document Object 
(
    [fields:protected] => Array 
     (
      [id] => 1 
      [title] => Array 
       (
        [0] => Getting Started with Solr, a simple Solr Tutorial 
       ) 

      [description] => The Unofficial Solr Guide: This simple Solr Tutorial covers basic Heliosearch and Apache Solr installation, Solr indexing, Solr search and querying. 
      [keywords] => solr,tutorial,guide,covers,indexing,search,query,installation,unofficial,apache 
      [url] => http://heliosearch.org/solr/getting-started/ 
      [_version_] => 1.4850885076533E+18 
      [score] => 1 
     ) 

) 

Я использую следующий PHP код

<?php 
      if($this->totalResults > 0) { 
       foreach($this->results as $result) { 
        //if(!empty($result->title) && !empty($result->url) && !empty($result->description)) { 
        if(isset($result->title) && isset($result->url) && isset($result->description)) { 
      ?> 
       <div class="search_result"> 
        <a class="title" href="#"> 
         <?php echo $result->title[0];?> 
        </a><br/> 
        <span class="url"> 
         <?php echo $result->url;?> 
        </span><br/> 
        <span class="description"> 
         <?php echo $result->description;?> 
        </span> 
       </div> 
       <?php } 
        } 
       }?> 

Я никогда не получаю никакого результата, но я должен получить последний узел, поскольку он содержит заголовок, URL и описание ключей.

Но когда я просто положить

if($result->title && $result->url && $result->description) { 

Затем он работает отлично. Почему он не работает в этом режиме.

enter image description here

ответ

0

Поскольку эти свойства отсутствуют, но заполнены с пустой строкой или нулем ... так isset() всегда верно для них ... если вы не 100% уверены, что они всегда есть, вы можете комбинировать обе проверки (isset($result->title) && $result->title). Альтернативой было бы что-то вроде (isset($result->title) && !empty($result->title)), которое тоже должно работать

+0

Я редактирую вопрос и размещаю весь объект –

3

Позвольте мне сказать вам, что isset работает только с переменными, а не с объектами. Пожалуйста, прочитайте документацию на php.net http://php.net/manual/en/function.isset.php. Я бы рекомендовал прочитать пример в нижней части страницы, где было объяснено, как isset всегда возвращает false, когда мы передаем ему объект.

+0

, но я уже упоминал в вопросе, что пустой тоже не работает. Тогда как насчет этого ??? –

+1

Если поля нет, то как он будет проверять пустую строку? Думаю об этом. –

+1

Попробуйте преобразовать объект в массив с помощью (array) ($ phpObj), тогда isset и! Empty начнет работать. Если вы найдете ответ полезным, вы можете пометить его ... :) –

Смежные вопросы