2017-09-03 91 views
0

我怎么会去这个PHP数组转换成C#转换一个PHP数组到C#

$hiscores = array("Overall", 
      "stats" => array(  
        "Attack", "Defence", "Strength", 
        "Hitpoints", "Ranged", "Prayer", 
        "Magic", "Cooking", "Woodcutting", 
        "Fletching", "Fishing", "Firemaking", 
        "Crafting", "Smithing", "Mining", 
        "Herblore", "Agility", "Thieving", 
        "Slayer", "Farming", "Runecrafting", 
        "Hunter", "Construction", "Summoning", 
        "Dungeoneering" 
        ), 
      "minigames" => array(
        "Duel Tournaments", "Bounty Hunters", 
        "Bounty Hunter Rogues", "Fist of Guthix", 
        "Mobilising Armies", "B.A Attackers", 
        "B.A Defenders", "B.A Healers", 
        "Castle Wars Games", "Conquest" 
      ) 
); 

我发现PHP语法很混乱,所以我不能告诉我怎么会去亲自做这个。

从我可以告诉它会是这样的:

string[] hiscores = new string["Overall","stats","minigames"] 

但是“统计”和“小游戏”不包含字符串喜欢他们在PHP代码做。

我也不确定“=> array()”是什么意思。

+1

取决于你想要用这个结构做什么。可能是一个'Dictionary >',可能是一个自定义类,可能是别的。不要挂在语言之间的逐行转换上。用目标语言实现功能。 – David

+0

我建议你阅读:http://php.net/manual/en/language.types.array.php –

回答

0

在PHP中的数组键和值,其定义如下:

key => "value" 

一个关键字可以有一个或多个关联的项目。在你的例子中,关键字有一个关联的字符串数组。

在C#中,你可以通过使用dictionary

白衣的字典,你可以字符串数组添加例如一个键做到这一点。现在你可以添加一系列迷你游戏到关键的迷你游戏中。要从小游戏中获得价值,你可以做dictionary["minigame"]。请注意,密钥必须存在于字典中

定义字典, 键将是字符串类型,并且绑定到键的值将是字符串数组的类型,因此您可以将字符串数组添加到一个键。

Dictionary<string, string[]> dictionary = new Dictionary<string, string[]>(); 

添加项目到词典:

 dictionary.Add("stats", new[] 
      { 
       "Attack", "Defence", "Strength", 
       "Hitpoints", "Ranged", "Prayer", 
       "Fletching", "Fishing", "Firemaking", 
       "Crafting", "Smithing", "Mining" 
      } 
     ); 

     dictionary.Add("minigames", new[] 
      { 
       "Duel Tournaments", "Bounty Hunters", 
       "Bounty Hunter Rogues", "Fist of Guthix", 
       "Mobilising Armies", "B.A Attackers", 
       "B.A Defenders", "B.A Healers", 
       "Castle Wars Games", "Conquest" 
      } 
     ); 

获取whits关联白衣项目stats键:

 foreach (var value in dictionary["stats"]) 
     { 
      Console.WriteLine(value); 
     } 

获取whits关联白衣项目minigames关键:

 foreach (var value in dictionary["minigames"]) 
     { 
      Console.WriteLine(value); 
     } 
0

考虑这个例子:

public class MakeList 
{ 
    private List<Model1> models1; 
    public MakeList() 
    { 
     models1 = new List<Model1> 
     { 
      new Model1 
      { 
       FirstProp = "Overall", 
       SecondProp = new List<String> 
         { 
          "Attack", "Defence", "Strength", 
         "Hitpoints", "Ranged", "Prayer", 
         "Magic", "Cooking", "Woodcutting", 
         "Fletching", "Fishing", "Firemaking", 
         "Crafting", "Smithing", "Mining", 
         "Herblore", "Agility", "Thieving", 
         "Slayer", "Farming", "Runecrafting", 
         "Hunter", "Construction", "Summoning", 
         "Dungeoneering" 
         }, 
       ThirdProp = new List<String> 
         { 
         "Duel Tournaments", "Bounty Hunters", 
         "Bounty Hunter Rogues", "Fist of Guthix", 
         "Mobilising Armies", "B.A Attackers", 
         "B.A Defenders", "B.A Healers", 
         "Castle Wars Games", "Conquest" 
         } 
     } 


    }; 
    } 
} 

有了这个类,你要改变,以适合您的需要:

public class Model1 
{ 
    public string FirstProp { get; set; } 
    public List<String> SecondProp { get; set; } 
    public List<String> ThirdProp { get; set; } 
}