简体   繁体   中英

Create complex Object PHP from array associative

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 >

$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 :

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

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!

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM