简体   繁体   中英

Why isset and not empty doesn't work in this Solarium object iteration

I'm trying to iterate the below object using foreach in PHP but I don't get any result.

 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
        )

)

I'm using the following PHP code

<?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 }
                    }
                }?>

I never get any result but I should get the last node because it contains title, url and description keys.

But when I simply put

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

Then it works fine. Why is it not working in that mode.

在此输入图像描述

Let me tell you that isset works only on variables and not for objects. Please read the documentation on php.net http://php.net/manual/en/function.isset.php . I would recommend reading the example at the bottom of the page where it has been explained how isset always returns false when we pass an object to it.

Because those properties are existent, but filled with an empty string or null... so isset() is always true for them... if you are not 100% sure, that they are always there, you could combine both checks (isset($result->title) && $result->title) . The alternative would be something like (isset($result->title) && !empty($result->title)) , which should work too

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM