简体   繁体   English

什么C#构造映射到PHP数组?

[英]What C# construct map to PHP arrays?

I will just list out some arrays and ask how to do them in C#. 我将列出一些数组,并询问如何在C#中进行处理。

$myArray = array();

In PHP I don't have to declare a memory size for the array. 在PHP中,我不必为数组声明内存大小。 What if I don't know big I need my array? 如果我不知道我需要我的阵列怎么办?

$myArray = array("Name" => "Steve");

How do I do the above in C#? 如何在C#中执行以上操作?

$myArray = array();
$myArray['Names'][0] = "Steve";
$myArray['Names'][1] = "Jim";

How does the above work in C#"? 以上内容如何在C#中工作?”?

$myArray = array("Name" => "Steve");

This is a map. 这是一张地图。 PHP doesn't need you to know that, but C# does. PHP不需要您知道这一点,但是C#不需要。 You would implement this as: 您可以将其实现为:

var myArray = new Dictionary<String, String>();

For your second case 对于第二种情况

$myArray['Names'][0] = "Steve";

This is a dictionary where the keys are Strings, but the values are String[] . 这是一个字典,其中的键是String,但是值是String[] In C#: 在C#中:

var myArray = new Dictionary<String, List<String>>();

arrays in PHP are most like maps or C# Dictionary . PHP中的数组最类似于map或C# Dictionary An array in PHP is actually what is called an associative array . PHP中的数组实际上就是所谓的associative array So for your code above the C# equivalent is: 因此,对于高于C#等价物的代码,它是:

        Dictionary<string, string> items= new Dictionary<string, string>();  
        items.Add("Name", "Steve");  

if you want a key to point to multiple values: 如果要一个键指向多个值:

        Dictionary<string, ICollection<string>> items= new  
                           Dictionary<string, ICollection<String>>();    
        ICollection<string> names = new List<string>();  
        names.Add("Steve");     
        items.Add("Name", names);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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