【问题标题】:Filtering an Array过滤数组
【发布时间】:2014-06-16 15:02:31
【问题描述】:

我正在开发一个显示属性的网站。我有一个巨大的 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;
}

但这不起作用。这将在一个名为 Results 的类中。我将如何搜索数组。我看了一下使用 array_filter();但我无法让它工作,当我 print_r 时它会返回 Array()。

有人知道如何搜索/过滤数组吗?

这是我 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] => 邮编 [displayAddress] => 地址 [propertyBedrooms] => 1 [propertyBathrooms] => 1 [propertyEnsuites] => 0 [propertyReceptionRooms] => 1 [propertyKitchens] => 1 [displayPropertyType] => Flat/Apartment [propertyType] => 2 [propertyStyle] => 16 [propertyAge] => 0 [floorArea] => 0.00 [floorAreaUnits] => 平方英尺 [propertyFeature1] => 改建校舍 [propertyFeature2] => 市中心位置 [propertyFeature3] => 现代配件 [propertyFeature4] => SimpleXMLElement Object () [propertyFeature5] => SimpleXMLElement Object () [propertyFeature6] => SimpleXMLElement Object () [propertyFeature7] => SimpleXMLElement Object ( ) [propertyFeature8] => SimpleXMLElement Object ( ) [propertyFeature9] => SimpleXMLElement Object ( ) [propertyFeature10] => SimpleXMLElement Object ( ) [rent] => 525 [rentFrequency] => 1 [toLetPOA] => 0 [studentProperty] => SimpleXMLElement Object () [可用性] => 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] => 图片)))

有谁知道我将如何搜索这个查询例如

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

等等。

【问题讨论】:

  • 你能不能像这样写一个print_r,这样我们可以更容易地阅读它? echo '<pre>'; print_r($array); echo '</pre>';?
  • 此外,您想要获取的示例——$post_code = 43; $min_price = 300; $max_price = 500——似乎不在您展示的数组中?

标签: php arrays class


【解决方案1】:

查看代码,逻辑对我来说似乎很好。 有一些东西可能会导致问题。 在代码的第 4 行,您有:

$this->properties[] = $property;

这意味着你有一个属性数组。

但是,您正在尝试直接访问该对象:

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

这对我来说看起来很奇怪。您正在尝试查看 abject 是否具有您想要的属性,然后将相同的对象保存为数组。

所以我猜,一旦您构建了 $this->properties 数组,您只需遍历它并应用您的过滤器以返回您正在寻找的属性数组:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-28
    • 2019-11-23
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 2019-11-08
    相关资源
    最近更新 更多