繁体   English   中英

为什么isset而不是空在此Solarium对象迭代中不起作用

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

我试图在PHP中使用foreach迭代下面的对象,但我没有得到任何结果。

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

我从来没有得到任何结果,但我应该得到最后一个节点,因为它包含标题,网址和描述键。

但是,当我简单地说

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

然后它工作正常。 为什么它不能在该模式下工作。

在此输入图像描述

让我告诉你,isset仅适用于变量而不适用于对象。 请阅读php.net http://php.net/manual/en/function.isset.php上的文档。 我建议阅读页面底部的示例,其中解释了当我们将对象传递给它时,isset总是返回false。

因为这些属性是存在的,但是填充了空字符串或null ...所以isset()对于它们总是如此...如果你不是100%确定,它们总是在那里,你可以组合两个检查(isset($result->title) && $result->title) 替代方案可能是(isset($result->title) && !empty($result->title)) ,这应该也适用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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