Well, there seems to be nothing built into .NET for you to do this. However, if you want to re-implement PHP behavior in .NET, you can either implement it, either look at the PHP source code, or implement it in a black box by reading the PHP Documentation http_build_query and testing the function on different inputs.
I took the black box approach and created the following class:
The code is pretty straightforward, but it takes a dictionary, array, or object. If it is a top-level object, it serialized the properties. If it is an array, each element is serialized with the corresponding array index. If it is a dictionary, key / values are serialized. Arrays and dictionary values containing other arrays or dictionaries are smoothed, as is the behavior of PHP.
For example, the following:
QueryStringBuilder.BuildQueryString(new { Age = 19, Name = "John&Doe", Values = new object[] { 1, 2, new Dictionary<string, string>() { { "key1", "value1" }, { "key2", "value2" }, } }, }); // 0=1&1=2&2%5B0%5D=one&2%5B1%5D=two&2%5B2%5D=three&3%5Bkey1%5D=value1&3%5Bkey2%5D=value2 QueryStringBuilder.BuildQueryString(new object[] { 1, 2, new object[] { "one", "two", "three" }, new Dictionary<string, string>() { { "key1", "value1" }, { "key2", "value2" }, } } );
Forms:
Age=19&Name=John%26Doe&Values%5B0%5D=1&Values%5B1%5D=2&Values%5B2%5D%5Bkey1%5D=value1&Values%5B2%5D%5Bkey2%5D=value2
which the:
Age=19&Name=John%26Doe&Values[0]=1&Values[1]=2&Values[2][key1]=value1&Values[2][key2]=value2 Age=19 Name=John&Doe Values[0]=1 Values[1]=2 Values[2][key1]=value1 Values[2][key2]=value2