Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert my PHP code to C# dimensional array

I have a data from database, that I want to put into an array for later process.

php version:

$data = [];
// $data[date][id] = [name, age];
$data["09/10/2023"][10] = ['name' => 'John Smith', 'age' => 50];
$data["09/10/2023"][11] = ['name' => 'Amie Kim', 'age' => 30];

How can I write the above code in C# ?
Later I want to loop through that data.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use a C# Dictionary to hold Person objects:

// Define a class to hold the name and age
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

// Then in your method or constructor
var data = new Dictionary<string, Dictionary<int, Person>>();

// Initialize the inner dictionary for the date "09/10/2023"
data["09/10/2023"] = new Dictionary<int, Person>();

// Add the persons
data["09/10/2023"][10] = new Person { Name = "John Smith", Age = 50 };
data["09/10/2023"][11] = new Person { Name = "Amie Kim", Age = 30 };
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading