Create complex Object PHP from array associative for information hotel reservation. In practice I have to recover some data from an XML response that I will need to show to the end user . I would create an object or something to help me to recover data quickly. When you call a function , like the one above you must return an object of easy access to all data and can be iterable by municipalities < foreach >
从数组关联创建复杂的对象PHP以获取信息酒店预订。在实践中,我必须从XML响应中恢复一些数据,我需要向最终用户显示这些数据。我会创建一个对象或东西来帮助我快速恢复数据。当您调用函数时,如上所述,您必须返回一个易于访问所有数据的对象,并且可以由市政当局迭代
$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;
}
Not if it is correct the way I wrote this type of object. There is an easier way to run a hotel with many rooms and then can access then in the simple form
如果我写这种类型的对象的方式不正确。有一个更简单的方法来经营一个有很多房间的酒店,然后可以通过简单的形式访问
$array_hotels[0]->name_hotel;
EDIT
I made these chnages as per an answer below
我根据下面的答案制作了这些琴
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;
}
Ok . I do not need to give as a parameter to the function getHotelList the hotel name ... I want to return a variable / object / array that will serve me in the next code . The result is that up to now :
好 。我不需要将函数getHotelList作为参数提供给酒店名称...我想返回一个变量/ object / array,它将在下一个代码中为我提供服务。结果是到目前为止:
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..
So far, so good. But how do I access the data?
到现在为止还挺好。但是我如何访问数据?
echo $res->nomeHotel;
echo $res[0]->nomeHotel;
echo $res[0]['nomeHotel'];
don't work
1 个解决方案
#1
0
I would do it like this
我会这样做的
$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 );
Of course you may need to check the property names are what you wanted, and I assumed the lastupdate
attributes name!
当然你可能需要检查你想要的属性名称,并且我假设了lastupdate属性名称!
UPDATE
As per your first attempt to implement my suggestion, now you have to pass the function a parameter to get it all started.
根据你第一次尝试实现我的建议,现在你必须传递一个参数来使它全部启动。
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);
UPDATE2
If you want to access the data structure in PHP, then just dont convert it to a JSON String, instead use it as a PHP data structure
如果要在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;
}
To see what the structure looks like all you need to do is a print_r($php_hotels);
要查看结构的外观,您需要做的就是print_r($ php_hotels);
#1
0
I would do it like this
我会这样做的
$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 );
Of course you may need to check the property names are what you wanted, and I assumed the lastupdate
attributes name!
当然你可能需要检查你想要的属性名称,并且我假设了lastupdate属性名称!
UPDATE
As per your first attempt to implement my suggestion, now you have to pass the function a parameter to get it all started.
根据你第一次尝试实现我的建议,现在你必须传递一个参数来使它全部启动。
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);
UPDATE2
If you want to access the data structure in PHP, then just dont convert it to a JSON String, instead use it as a PHP data structure
如果要在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;
}
To see what the structure looks like all you need to do is a print_r($php_hotels);
要查看结构的外观,您需要做的就是print_r($ php_hotels);