0

I am a c# guy and working with an api where all examples are in php so i need help so i can understand what is this code exactly

$rooms = array();

     // First Room
     $rooms[] = array(array("paxType" => "Adult"));

     // Second Room
     $rooms[] = array(array("paxType" => "Adult"), array("paxType" => "Adult"), array("paxType" => "Child", "age" => 8));

I need if anybody can describe this code to me as i am c# guy.

As i am google around this so this is a multidimensional array that i already understand

and pax is class in this api (as per api documentation) which have some properties like paxtype,age etc.....

but i am not getting the way to right this in c#.

Edit

that pax type multidimensional array passed to this method

public getAvailableHotelResponse getAvailableHotel(string apiKey, string destinationId, DateTime checkIn, DateTime checkOut, string currency, string clientNationality, bool onRequest, pax[][] rooms, filter[] filters);

2nd EDIT

public getAvailableHotelResponse getAvailableHotel(string apiKey, string destinationId, DateTime checkIn, DateTime checkOut, string currency, string clientNationality, bool onRequest, pax[][] rooms, filter[] filters);

I have to pass this pax[][] rooms in my method and rooms will have the following structure

rooms[0][0][paxType]=Adult
rooms[0][1][paxType]=Adult
rooms[0][2][paxType]=Child
rooms[0][2][age]=6


rooms[1][0][paxType]=Adult
rooms[1][1][paxType]=Adult
rooms[1][2][paxType]=Child
rooms[1][2][age]=8

and the pax class is below

 public class pax
    {
        public pax();

        [SoapElement(DataType = "integer")]
        public string age { get; set; }
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string paxType { get; set; }
        public string title { get; set; }
    }

I think its now much clearer to us all.

Czar Pino
  • 6,258
  • 6
  • 35
  • 60
rahularyansharma
  • 11,156
  • 18
  • 79
  • 135

2 Answers2

1

Arrays are dynamic in PHP. Since one cannot determine the size, translating it to C# will require a non-primitive array.

// $rooms = array ();
List<List<pax>> rooms = new List<List<pax>>();

The ff PHP code,

$rooms[] = array (array ("paxType" => "Adult"));

can also be interpreted as:

$room = array ();
$pax = array ();
$pax["paxType"] = "Adult";
$room[] = $pax;
$rooms[] = $room;

Hence,

// $room = array ();
List<pax> room = new List<pax>();

// $pax = array ();
pax p = new pax();

// $pax["paxType"] = "Adult";
p.paxType = "Adult";

// $room[] = $pax;
room.Add(p);

// $rooms[] = $room;
rooms.Add(room);

Then just convert it to a primitive array.

// this will be the pax type array
pax[][] paxRooms = new pax[rooms.count][];
for (int i = 0; i < rooms.Count; i++) {
    paxRooms[i][] = rooms[i].ToArray();
}

P.S. I'm a PHP/Java guy so excuse me for syntax errs.

Czar Pino
  • 6,258
  • 6
  • 35
  • 60
-1

The $rooms data structure looks like this:

[
  [{paxType:"Adult"}],
  [{paxType:"Adult"}, {paxType:"Adult"}, {paxType:"Child", age:8}]
]
troelskn
  • 115,121
  • 27
  • 131
  • 155