簡體   English   中英

過濾數組

[英]Filtering an Array

我正在一個顯示屬性的網站上。 我有一個巨大的XML提要,我將其放入一個數組中。

function search_results($department = '', $post_code = '', $min_price = '', $max_price = '', $type = '', $bedrooms = '') {
    foreach($this->xml->property as $property) {
        if($property->department == $department) {
            $this->properties[] = $property; 
        }
    }
    $this->filter_results();
}

這首先根據部門(例如“待售”或“待租”)過濾XML。 我現在希望能夠基於通過函數傳遞的變量來搜索此數組。 例如:

if($this->properties->regionID == $post_code) {
    $this->properties[] = $property;
}

但這是行不通的。 這將在名為“結果”的類中。 我將如何搜索數組。 我看過使用array_filter(); 但是我無法正常工作,當我print_r它會返回Array()。

有誰知道如何搜索/過濾數組?

這是我print_r時數組的樣子:

數組([0] => SimpleXMLElement對象([propertyID] => 1 [branchID] => 1 [clientName] =>名稱[branchName] =>分支[部門] =>出租[referenceNumber] => 1 [addressName] = > 4 [addressNumber] => SimpleXMLElement對象()[addressStreet] =>地址[address2] => address2 [address3] => address3 [address4] => SimpleXMLElement對象()[addressPostcode] =>郵政編碼[country] =>郵政編碼[displayAddress] =>地址[propertyBedrooms] => 1 [propertyBathrooms] => 1 [propertyEnsuites] => 0 [propertyReceptionRooms] => 1 [propertyKitchens] => 1 [displayPropertyType] =>公寓/公寓[propertyType] => 2 [propertyStyle] => 16 [propertyAge] => 0 [floorArea] => 0.00 [floorAreaUnits] =>平方英尺[propertyFeature1] =>改建后的校舍[propertyFeature2] =>市中心位置[propertyFeature3] =>現代配件[propertyFeature4 ] => SimpleXMLElement對象()[propertyFeature5] => SimpleXMLElement對象()[propertyFeature6] => SimpleXMLElement對象()[propertyFeature7] => SimpleXMLElement對象()[propertyFeature8] => SimpleXMLElement對象()[propertyFeature9] => SimpleXMLElement對象()[propertyFeature10] => SimpleXMLElement對象()[rent] => 525 [rentFrequency] => 1 [toLetPOA] => 0 [ studentProperty] => SimpleXMLElement對象()[可用性] => 2 [mainSummary] =>摘要。 [fullDescription] => SimpleXMLElement對象()[dateLastModified] => 2014-05-02 [featuredProperty] => 0 [regionID] => 27 [緯度] => SimpleXMLElement對象()[經度] => SimpleXMLElement對象()[標志] => SimpleXMLElement對象()[圖像] => SimpleXMLElement對象([圖像] =>數組([0] =>圖像[1] =>圖像[2] =>圖像[3] =>圖像[4] =>圖片)))

有誰知道我將如何搜索此查詢,例如

$post_code = 43; $min_price = 300; $max_price = 500

等等。

看代碼,邏輯對我來說似乎很好。 盡管有可能導致此問題的原因。 在代碼的第4行上,您具有:

$this->properties[] = $property;

這意味着您具有一系列屬性。

但是,然后,您嘗試直接訪問該對象:

if($this->properties->regionID == $post_code) {
    $this->properties[] = $property;
}

這在我看來很奇怪。 您試圖查看對象是否具有所需的屬性,然后將同一對象保存為數組。

因此,我猜想,一旦構建了$this->properties數組,您只需要遍歷它並應用過濾器即可返回具有所需內容的屬性數組:

foreach($this->properties as $singleProperty) 
{
    if($singleProperty->regionID == $post_code) 
    {
        $this->foundProperties[] = $singleProperty;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM