我正在开发一个属性网站,该网站从CRM数据库中提取结果。撤回的结果是基于较大房产内的可用单位(即包含各种公寓的一个大街区)。我需要能够按每个属性推送一个图像数组但不是每个单元,但结果数组会带回每个属性和每个单元。这就是我的意思:
Array
(
[0] => stdClass Object
(
[property_id] => 2
[unit_id] => 5
[property_name] => Morb House
[address] => 123 Test Street, Blahtown, BL1 AHH
[unit_name] => Apartment Number 1
[max_guests] => 2
[description] => This is a nice place
[bedrooms] => 1
)
[1] => stdClass Object
(
[property_id] => 2
[unit_id] => 6
[property_name] => Morb House
[address] => Some Road, Some Town, Anywhere
[unit_name] => Apartment Number 2
[max_guests] => 4
[description] => This is a slightly bigger place
[bedrooms] => 2
)
[2] => stdClass Object
(
[property_id] => 2
[unit_id] => 11
[property_name] => Morb House
[address] => 123 Test Street, Blahtown, BL1 AHH
[unit_name] => Cool Studio
[max_guests] => 1
[description] => This is a teeny weeny place
[bedrooms] => 1
)
[3] => stdClass Object
(
[property_id] => 4
[unit_id] => 12
[property_name] => Fake Buildings
[address] => 1 Fake Street, Anytown
[unit_name] => Fake Apartment One
[max_guests] => 1
[description] => Some description here
[bedrooms] => 1
)
)
我将从另一个表中获取的图像将按属性ID进行过滤,我打算将其添加到与该属性相关的对象中。但是,由于可以在单个属性中预订多个单元,因此该属性ID在前3个结果中重复。理想情况下,我希望我的结果看起来像这样:
Array
(
[2] => stdClass Object
(
[property_id] => 2
[property_name] => Morb House
[address] => 123 Test Street, Blahtown, BL1 AHH
[units] => Array
(
[5] => stdClass Object
(
[unit_name] => Apartment Number 1
[max_guests] => 2
[description] => This is a nice place
[bedrooms] => 1
)
[6] => stdClass Object
(
[unit_name] => Apartment Number 2
[max_guests] => 4
[description] => This is a slightly bigger place
[bedrooms] => 2
)
[11] => stdClass Object
(
[unit_name] => Cool Studio
[max_guests] => 1
[description] => This is a teeny weeny place
[bedrooms] => 1
)
)
)
[4] => stdClass Object
(
[property_id] => 4
[property_name] => Fake Buildings
[address] => 1 Fake Street, Anytown
[units] => Array
(
[12] => stdClass Object
(
[unit_name] => Fake Apartment One
[max_guests] => 1
[description] => Some description here
[bedrooms] => 1
)
)
)
)
如果密钥实际上是属性ID,并且该属性中的每个单元都有一个子对象,则由相关的单元ID键入。
我只需要在PHP中简单而干净地提供一些如何做到这一点的建议。
答案 0 :(得分:2)
$out=array();
foreach($array as $item)
{
$new = new StdClass;
foreach(array('unit_name','max_guests','description','bedrooms') as $add){
$new->{$add} = $item->{$add};
}
if(isset($item[$item->property_id])){
$out[$item->property_id]->units[$item->unit_id] = $new;
continue;
}
$out[$item->property_id] = (object)array('property_id'=>$item->property_id,'property_name'=>$item->property_name,'address'=>$item->address,'units'=>array($item->unit_id=>$new));
}
print_r($out);
答案 1 :(得分:0)
您可以获得所需格式的唯一方法是,如果您使用ORM并为您的属性和单位表设置了适当的关系设置。如果没有,那么你必须将现有结果转换为你想要的格式。
e.g。
<?php
// your current result set goes here
$units = $your_crm->database->get_units();
$properties = [];
foreach ($units as $_unit)
{
$id = $_unit->property_id;
$unit = [
'unit_name' => $_unit->unit_name,
'max_guests' => $_unit->max_guests,
'description' => $_unit->description,
'bedrooms' => $_unit->bedrooms,
];
if (isset($properties[$id])) {
$properties[$id]->units[] = $unit;
} else {
$properties[$id] = (object) [
'property_id' => $_unit->property_id,
'property_name' => $_unit->property_name,
'address' => $_unit->address,
'units' => [$unit]
];
}
}
print_r($properties);