简体   繁体   English

过滤数组

[英]Filtering an Array

I'm working on a website that displays properties. 我正在一个显示属性的网站上。 I have a huge XML feed in which I put into 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();
}

this first filters the XML based on the department such as 'For Sale' or 'To Rent'. 这首先根据部门(例如“待售”或“待租”)过滤XML。 I want to now be able to search this array based on the variables I pass though the function. 我现在希望能够基于通过函数传递的变量来搜索此数组。 For example: 例如:

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

But this doesn't work. 但这是行不通的。 This will be in a class called Results. 这将在名为“结果”的类中。 How will I go about searching the array. 我将如何搜索数组。 I had a look at using array_filter(); 我看过使用array_filter(); but I couldn't get it working it would return Array() when I print_r it. 但是我无法正常工作,当我print_r它会返回Array()。

Does anyone know how to search/filter an array? 有谁知道如何搜索/过滤数组?

This is what the array looks like when I print_r: 这是我print_r时数组的样子:

Array ( [0] => SimpleXMLElement Object ( [propertyID] => 1 [branchID] => 1 [clientName] => Name [branchName] => Branch [department] => Lettings [referenceNumber] => 1 [addressName] => 4 [addressNumber] => SimpleXMLElement Object ( ) [addressStreet] => address [address2] => address2 [address3] => address3 [address4] => SimpleXMLElement Object ( ) [addressPostcode] => postcode [country] => postcode [displayAddress] => address [propertyBedrooms] => 1 [propertyBathrooms] => 1 [propertyEnsuites] => 0 [propertyReceptionRooms] => 1 [propertyKitchens] => 1 [displayPropertyType] => Flat/Apartment [propertyType] => 2 [propertyStyle] => 16 [propertyAge] => 0 [floorArea] => 0.00 [floorAreaUnits] => sq ft [propertyFeature1] => Converted School House [propertyFeature2] => City Centre Location [propertyFeature3] => Modern Fittings [propertyFeature4] => SimpleXMLElement Object ( ) [propertyFeature5] => SimpleXMLElement Object ( ) [propertyFeature6] => SimpleXMLElement Object ( ) [propertyFeature7] => 数组([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 Object ( ) [propertyFeature8] => SimpleXMLElement Object ( ) [propertyFeature9] => SimpleXMLElement Object ( ) [propertyFeature10] => SimpleXMLElement Object ( ) [rent] => 525 [rentFrequency] => 1 [toLetPOA] => 0 [studentProperty] => SimpleXMLElement Object ( ) [availability] => 2 [mainSummary] => summary. SimpleXMLElement对象()[propertyFeature8] => SimpleXMLElement对象()[propertyFeature9] => SimpleXMLElement对象()[propertyFeature10] => SimpleXMLElement对象()[rent] => 525 [rentFrequency] => 1 [toLetPOA] => 0 [ studentProperty] => SimpleXMLElement对象()[可用性] => 2 [mainSummary] =>摘要。 [fullDescription] => SimpleXMLElement Object ( ) [dateLastModified] => 2014-05-02 [featuredProperty] => 0 [regionID] => 27 [latitude] => SimpleXMLElement Object ( ) [longitude] => SimpleXMLElement Object ( ) [flags] => SimpleXMLElement Object ( ) [images] => SimpleXMLElement Object ( [image] => Array ( [0] => image [1] => image [2] => image [3] => image [4] => image ) )) [fullDescription] => SimpleXMLElement对象()[dateLastModified] => 2014-05-02 [featuredProperty] => 0 [regionID] => 27 [纬度] => SimpleXMLElement对象()[经度] => SimpleXMLElement对象()[标志] => SimpleXMLElement对象()[图像] => SimpleXMLElement对象([图像] =>数组([0] =>图像[1] =>图像[2] =>图像[3] =>图像[4] =>图片)))

Does anyone know how I would be able to search this query for example 有谁知道我将如何搜索此查询,例如

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

and so on. 等等。

Looking at the code, the logic seems good to me. 看代码,逻辑对我来说似乎很好。 There is something though that might cause the issue. 尽管有可能导致此问题的原因。 On line 4 of your code, you have: 在代码的第4行上,您具有:

$this->properties[] = $property;

It means that you have an array of properties. 这意味着您具有一系列属性。

But then, you are trying to access the object directly: 但是,然后,您尝试直接访问该对象:

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

This looks strange to me. 这在我看来很奇怪。 You are trying to see if an abject has the property you want, and then save the same object as it is an array. 您试图查看对象是否具有所需的属性,然后将同一对象保存为数组。

So I guess, once you have built your $this->properties array, you just have to loop through it and apply your filters to return an array of properties with just what you are looking for: 因此,我猜想,一旦构建了$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