我正在开发一个显示属性的网站。我有一个巨大的XML feed,我把它放入一个数组中。
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();
}
首先根据“For Sale”或“To Rent”等部门过滤XML。我想现在能够根据我通过函数传递的变量来搜索这个数组。例如:
if($this->properties->regionID == $post_code) {
$this->properties[] = $property;
}
但这不起作用。这将在名为Results的类中。我将如何搜索阵列。我看了一下使用array_filter();但我无法使它工作,当我print_r它时会返回Array()。
有谁知道如何搜索/过滤数组?
这是print_r时数组的样子:
数组([0] =&gt; SimpleXMLElement对象([propertyID] =&gt; 1 [branchID] =&gt; 1 [clientName] =&gt;名称[branchName] =&gt;分支[部门] =&gt;租赁[ referenceNumber] =&gt; 1 [addressName] =&gt; 4 [addressNumber] =&gt; SimpleXMLElement对象()[addressStreet] =&gt;地址[address2] =&gt; address2 [address3] =&gt; address3 [address4] =&gt; SimpleXMLElement Object()[addressPostcode] =&gt; postcode [country] =&gt; postcode [displayAddress] =&gt; address [propertyBedrooms] =&gt; 1 [propertyBathrooms] =&gt; 1 [propertyEnsuites] =&gt; 0 [propertyReceptionRooms] = &gt; 1 [propertyKitchens] =&gt; 1 [displayPropertyType] =&gt;单位/公寓[propertyType] =&gt; 2 [propertyStyle] =&gt; 16 [propertyAge] =&gt; 0 [floorArea] =&gt; 0.00 [floorAreaUnits] =&gt; sq ft [propertyFeature1] =&gt;已转换的校舍[propertyFeature2] =&gt;市中心位置[propertyFeature3] =&gt;现代装修[propertyFeature4] =&gt; SimpleXMLElement对象()[propertyFeature5] =&gt; S impleXMLElement Object()[propertyFeature6] =&gt; SimpleXMLElement Object()[propertyFeature7] =&gt; SimpleXMLElement Object()[propertyFeature8] =&gt; SimpleXMLElement Object()[propertyFeature9] =&gt; SimpleXMLElement Object()[propertyFeature10] =&gt; SimpleXMLElement Object()[rent] =&gt; 525 [rentFrequency] =&gt; 1 [toLetPOA] =&gt; 0 [studentProperty] =&gt; SimpleXMLElement Object()[availability] =&gt; 2 [mainSummary] =&gt;摘要。 [fullDescription] =&gt; SimpleXMLElement Object()[dateLastModified] =&gt; 2014-05-02 [featuredProperty] =&gt; 0 [regionID] =&gt; 27 [纬度] =&gt; SimpleXMLElement Object()[longitude] =&gt; SimpleXMLElement Object()[flags] =&gt; SimpleXMLElement Object()[images] =&gt; SimpleXMLElement对象([image] =&gt;数组([0] =&gt;图像[1] =&gt;图像[2] =&gt;图像[3] =&gt;图像[4] =&gt;图像)))< / p>
有谁知道我将如何搜索此查询,例如
$post_code = 43; $min_price = 300; $max_price = 500
等等。
答案 0 :(得分:0)
查看代码,逻辑对我来说似乎很好。 虽然可能会导致问题。 在代码的第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;
}
}