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.

9
  • Yes, you've made a multidimensional array. What exactly is the question? Commented Jul 15, 2012 at 13:01
  • @Utkanos how to right this in c# Commented Jul 15, 2012 at 13:02
  • 1
    I am a PHP'er not a C guy, my understanding is a multidimensional array in any language is a multidimensional array. Commented Jul 15, 2012 at 13:02
  • @Lion yes because example which i have in php and i have to write this code in c# Commented Jul 15, 2012 at 13:02
  • i think this question is goinig to close as non constructive and i do not get my answer Commented Jul 15, 2012 at 13:03

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

@its now working for me dear . I just need a pax[][] type array . as you can see in my method signature
@rahularyansharma what is working? once you have the rooms List then just convert it to a pax array. won't the idea of last code block work?
i write this code List<pax> room = new List<pax>(); pax p1 = new pax(); p1.paxType="Adult"; room.Add(p1); pax[][] p2 = new pax[room.Count][]; for (int i = 0; i < room.Count; i++) { p2[i][] = room[i].ToArray(); }
@rahularyansharma What's the problem with that? All that's left is to loop for every room. foreach (List<pax> room in rooms) { /*your code here */}
is it possible to come in chat room
|
-1

The $rooms data structure looks like this:

[
  [{paxType:"Adult"}],
  [{paxType:"Adult"}, {paxType:"Adult"}, {paxType:"Child", age:8}]
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.