从数组关联创建复杂的对象PHP以获取信息酒店预订。 在实践中,我必须从XML响应中恢复一些数据,我需要向最终用户显示这些数据。 我会创建一个对象或东西来帮助我快速恢复数据。 当您调用函数时,如上所述,您必须返回一个易于访问所有数据的对象,并且可以由市政当局进行迭代。 foreach>
$resXML = $test->sendXML($xml_req);
//$dom = new DOMDocument;
$dom = new DOMDocument('1.0');
$dom->loadXML($resXML);
$success = $dom->getElementsByTagName('success');
$error = $dom->getElementsByTagName('error');
$hotels = $dom->getElementsByTagName('hotel');
$room = $dom->getElementsByTagName('room');
$arr_hotels = [];
$arr_rooms = [];
$n_hotel=0;
foreach($hotels as $hotel){
$idHotel = $hotel->getAttribute('xsd:id');
$nomeHotel = $hotel->getAttribute('xsd:name');
//echo "<strong>Nome Hotel: </strong>" . $hotel->getAttribute('xsd:name')."<br>";
$arr_hotels[] = ['idHotel'=>$idHotel, 'nomeHotel'=>$nomeHotel];
$rooms = $hotel->getElementsByTagName('room');
foreach($rooms as $room){
/*
echo "Prezzo: " . $room->getAttribute('xsd:price')."<br>";
$boardType = $room->getElementsByTagName('boardType');
$roomType = $room->getElementsByTagName('roomType');
echo "Nome Gruppo: " . $boardType[0]->getAttribute('xsd:groupName')."<br>";
echo "Tipo Stanza: " . $roomType[0]->getAttribute('xsd1:nameEn')."<br>";
*/
$boardType = $room->getElementsByTagName('boardType');
$roomType = $room->getElementsByTagName('roomType');
$idRoom = $room->getAttribute('xsd:id'); // Id della stanza
$prezzo = $room->getAttribute('xsd:price'); // prezzo della stanza
$idGruppo = $boardType[0]->getAttribute('xsd:groupId'); // Id Gruppo tipo di stanza
$nomeGruppo = $boardType[0]->getAttribute('xsd:groupName'); // nome gruppo tipo stanza
$idTipo = $boardType[0]->getAttribute('xsd1:id'); // Tipo specifico di stanza Colazione, Lunch
$nomeTipo = $roomType[0]->getAttribute('xsd1:nameEn'); // nome del tipo
$arr_hotels[]= array('idRoom'=>$idRoom,'prezzo'=>$prezzo);
}
$n_hotel++;
return $arr_hotels;
}
如果我写这种类型的对象的方式不正确。 有一个更简单的方法来经营一个有很多房间的酒店,然后可以通过简单的形式访问
$array_hotels[0]->name_hotel;
我根据下面的答案制作了这些内容
function getHotelList(){
foreach($hotels as $hotel){
$h = new stdClass();
$h->idHotel = $hotel->getAttribute('xsd:id');
$h->nomeHotel = $hotel->getAttribute('xsd:name');
$h->last_update = $hotel->getAttribute('xsd:lastUpdated');
$rooms = $hotel->getElementsByTagName('room');
//$idHotel = $hotel->getAttribute('xsd:id');
//$nomeHotel = $hotel->getAttribute('xsd:name');
//echo "<strong>Nome Hotel: </strong>" . $hotel->getAttribute('xsd:name')."<br>";
//$arr_hotels[] = ['idHotel'=>$idHotel, 'nomeHotel'=>$nomeHotel];
//$rooms = $hotel->getElementsByTagName('room');
foreach($rooms as $room){
/*
echo "Prezzo: " . $room->getAttribute('xsd:price')."<br>";
$boardType = $room->getElementsByTagName('boardType');
$roomType = $room->getElementsByTagName('roomType');
echo "Nome Gruppo: " . $boardType[0]->getAttribute('xsd:groupName')."<br>";
echo "Tipo Stanza: " . $roomType[0]->getAttribute('xsd1:nameEn')."<br>";
*/
$boardType = $room->getElementsByTagName('boardType');
$roomType = $room->getElementsByTagName('roomType');
$r = new stdClass();
// Stanza
$r->roomId = $room->getAttribute('xsd:id'); // Id della stanza
$r->prezzo = $room->getAttribute('xsd:price'); // prezzo della stanza
// Board Type
$r->idGruppo = $boardType[0]->getAttribute('xsd:groupId');
$r->nomeGruppo = $boardType[0]->getAttribute('xsd:groupName');
$r->idTipoServizi = $boardType[0]->getAttribute('xsd1:id');
// Room Type
$r->idRoomType = $roomType[0]->getAttribute('xsd1:id');
$r->nomeRoomType = $roomType[0]->getAttribute('xsd:hotelstonName');
$h->rooms[] = $r;
/*
$idRoom = $room->getAttribute('xsd:id'); // Id della stanza
$prezzo = $room->getAttribute('xsd:price'); // prezzo della stanza
$idGruppo = $boardType[0]->getAttribute('xsd:groupId'); // Id Gruppo tipo di stanza
$nomeGruppo = $boardType[0]->getAttribute('xsd:groupName'); // nome gruppo tipo stanza
$idTipo = $boardType[0]->getAttribute('xsd1:id'); // Tipo specifico di stanza Colazione, Lunch
$nomeTipo = $roomType[0]->getAttribute('xsd1:nameEn'); // nome del tipo
$arr_rooms[$nomeHotel][]= array('idRoom'=>$idRoom,'prezzo'=>$prezzo);
*/
}
$n_hotel++;
$arr_hotels[] = $h;
}
$res = json_encode( $arr_hotels );
return $res;
}
好的。我不需要将函数getHotelList作为参数给出酒店名称... 我想返回一个变量/对象/数组,它将在下一个代码中为我提供服务。 结果是到目前为止:
string(42910) "[{"idHotel":"50168124","nomeHotel":"Principe di Villafranca","last_update":"2016-09-18T23:29:02.927+03:00","rooms":[{"roomId":"11660402283","prezzo":"2788.59","idGruppo":"30309841","nomeGruppo":"Breakfast","idTipoServizi":"2359298","idRoomType":"70372446","nomeRoomType":"Superior Room - Double Bed"},{"roomId":"11660402283","prezzo":"2788.59","idGruppo":"30309841","nomeGruppo":"Breakfast","idTipoServizi":"2359298","idRoomType":"70372455","nomeRoomType":"Superior Room 2 Twin Beds"}]},{"idHotel":"50280829","nomeHotel":"Villa D'Amato","last_update":"2016-09-18T23:32:58.307+03:00","rooms":[{"roomId":"11660402116","prezzo":"127.14","idGruppo":"30309841","nomeGruppo":"Breakfast","idTipoServizi":"2359298","idRoomType":"70435376","nomeRoomType":"Standard Room Double - For Single Use, Double Bed"},{"roomId":"11660402118","prezzo":"137.74","idGruppo":"30309841","nomeGruppo":"Breakfast","idTipoServizi":"2359298","idRoomType":"70435429","nomeRoomType":"Superior Room Double - For Single Use, Double Bed"},{"roomId":"11660402287","prezzo":"147.60","idGruppo":"30309841","nomeGruppo":"Breakfast","idTipoServizi":"2359298","idRoomType":"52679712","nomeRoomType":"Standard Room"},{"roomId":"11660402121","prezzo":"148.34","idGruppo":"30309841","nomeGruppo":"Breakfast","idTipoServizi":"2359298","idRoomType":"70217317","nomeRoomType":"Standard Room Double Or Twin - Double Bed"},{"roomId":"11660402121","prezzo":"148.34","idGruppo":"30309841","nomeGruppo":"Breakfast","idTipoServizi":"2359298","idRoomType":"70217318","nomeRoomType":"Standard Room Double Or Twin"},{"roomId":"11661337064","prezzo":"151.39","idGruppo":"30309841","nomeGruppo":"Breakfast","idTipoServizi":"30309162","idRoomType":"65028065","nomeRoomType":"Standard Room Double Or Twin"},{"roomId":"11660402124","prezzo":"158.92","idGruppo":"30309841","nomeGruppo":"Breakfast","idTipoServizi":"2359298","idRoomType":"70217246","nomeRoomType":"Deluxe Room Double Or Twin - Double Bed"},etc..
到目前为止,这么好。 但是如何访问数据呢?
echo $res->nomeHotel;
echo $res[0]->nomeHotel;
echo $res[0]['nomeHotel'];
不能工作
答案 0 :(得分:0)
我会这样做
$resXML = $test->sendXML($xml_req);
//$dom = new DOMDocument;
$dom = new DOMDocument('1.0');
$dom->loadXML($resXML);
$success = $dom->getElementsByTagName('success');
$error = $dom->getElementsByTagName('error');
$hotels = $dom->getElementsByTagName('hotel');
$room = $dom->getElementsByTagName('room');
$arr_hotels = [];
//$n_hotel=0; does not appear to be used
foreach($hotels as $hotel){
$h = new stdClass();
$h->idHotel = $hotel->getAttribute('xsd:id');
$h->nomeHotel = $hotel->getAttribute('xsd:name');
$h->last_update = $hotel->getAttribute('xsd:lastupdate');
$rooms = $hotel->getElementsByTagName('room');
foreach($rooms as $room){
$r = new stdClass();
$r->price = $room->getAttribute('xsd:price');
$r->boardType = $room->getElementsByTagName('boardType');
$r->roomType = $room->getElementsByTagName('roomType');
$r->groupname = $boardType[0]->getAttribute('xsd:groupName');
$r->nameEn = $roomType[0]->getAttribute('xsd1:nameEn');
$r->idRoom = $room->getAttribute('xsd:id'); // Id della stanza
$r->prezzo = $room->getAttribute('xsd:price'); // prezzo della stanza
$r->idGruppo = $boardType[0]->getAttribute('xsd:groupId');
$r->nomeGruppo = $boardType[0]->getAttribute('xsd:groupName');
$r->idTipo = $boardType[0]->getAttribute('xsd1:id');
$r->nomeTipo = $roomType[0]->getAttribute('xsd1:nameEn');
$h->rooms[] = $r;
}
$arr_hotels[] = $h;
//$n_hotel++; Does not appear to be used
}
echo json_encode( $arr_hotels );
当然你可能需要检查你想要的属性名称,我假设lastupdate
属性名称!
根据你第一次尝试实现我的建议,现在你必须传递一个参数来使它全部启动。
function getHotelList($hotels){
$arr_hotels[];
foreach($hotels as $hotel){
$h = new stdClass();
$h->idHotel = $hotel->getAttribute('xsd:id');
$h->nomeHotel = $hotel->getAttribute('xsd:name');
$h->last_update = $hotel->getAttribute('xsd:lastUpdated');
$rooms = $hotel->getElementsByTagName('room');
foreach($rooms as $room){
$boardType = $room->getElementsByTagName('boardType');
$roomType = $room->getElementsByTagName('roomType');
$r = new stdClass();
$r->roomId = $room->getAttribute('xsd:id');
$r->prezzo = $room->getAttribute('xsd:price');
$r->idGruppo = $boardType[0]->getAttribute('xsd:groupId');
$r->nomeGruppo = $boardType[0]->getAttribute('xsd:groupName');
$r->idTipoServizi = $boardType[0]->getAttribute('xsd1:id');
$r->idRoomType = $roomType[0]->getAttribute('xsd1:id');
$r->nomeRoomType = $roomType[0]->getAttribute('xsd:hotelstonName');
$h->rooms[] = $r;
}
$n_hotel++;
$arr_hotels[] = $h;
}
return json_encode( $arr_hotels );
}
$resXML = $test->sendXML($xml_req);
$dom = new DOMDocument('1.0');
$dom->loadXML($resXML);
$success = $dom->getElementsByTagName('success');
$error = $dom->getElementsByTagName('error');
$hotels = $dom->getElementsByTagName('hotel');
$json_string = getHotelList($hotels);
如果要访问PHP中的数据结构,那么就不要将其转换为JSON字符串,而是将其用作PHP数据结构
function getHotelList($hotels){
$arr_hotels[];
foreach($hotels as $hotel){
$h = new stdClass();
$h->idHotel = $hotel->getAttribute('xsd:id');
$h->nomeHotel = $hotel->getAttribute('xsd:name');
$h->last_update = $hotel->getAttribute('xsd:lastUpdated');
$rooms = $hotel->getElementsByTagName('room');
foreach($rooms as $room){
$boardType = $room->getElementsByTagName('boardType');
$roomType = $room->getElementsByTagName('roomType');
$r = new stdClass();
$r->roomId = $room->getAttribute('xsd:id');
$r->prezzo = $room->getAttribute('xsd:price');
$r->idGruppo = $boardType[0]->getAttribute('xsd:groupId');
$r->nomeGruppo = $boardType[0]->getAttribute('xsd:groupName');
$r->idTipoServizi = $boardType[0]->getAttribute('xsd1:id');
$r->idRoomType = $roomType[0]->getAttribute('xsd1:id');
$r->nomeRoomType = $roomType[0]->getAttribute('xsd:hotelstonName');
$h->rooms[] = $r;
}
$n_hotel++;
$arr_hotels[] = $h;
}
return $arr_hotels;
}
$resXML = $test->sendXML($xml_req);
$dom = new DOMDocument('1.0');
$dom->loadXML($resXML);
$success = $dom->getElementsByTagName('success');
$error = $dom->getElementsByTagName('error');
$hotels = $dom->getElementsByTagName('hotel');
$php_hotels = getHotelList($hotels);
// print_r($php_hotels); // do this to see the data structure
foreach ( $php_hotels as $ph ) {
echo $ph->nomeHotel;
}
要查看结构的外观,您需要做的只是print_r($php_hotels);