Как использовать словарь типа Array в Php - PullRequest
1 голос
/ 13 апреля 2019

Я хочу использовать массив словарных типов в php, где я могу иметь ключ, а значение - это массив.я написал некоторый код на C #, но я не знаю, как добиться этого в php laravel.Кто-нибудь знает ответ, пожалуйста, поделитесь со мной.Спасибо

Только эта часть:

      if (invoiceDictionary.ContainsKey(customerId))
      {
        List<Invoice> lstinvoices = invoiceDictionary[customerId]; 
        // get the  list by key 
        var discreteInvoces = lstinvoices.Where(x => x.invoiceNumber != 
        invoiceNumber).ToList(); // get all invoices execpt this
        invoiceDictionary[customerId] = discreteInvoces;
      }
      else
      {
        var discreteInvoces = getListInvoices().Where(x => x.invoiceNumber 
        != invoiceNumber).ToList(); //get all the invoice except this
        invoiceDictionary.Add(customerId, discreteInvoces);
        } 
  1. У меня есть класс

    public class Invoice
    {
    public string invoiceNumber { get; set; }
    public string customerId { get; set; }
    }
    
  2. У меня есть списокОбъект счета

     public List<Invoice> getListInvoices()
     {
    
        // just adding few invoices in the list 
        List<Invoice> lstinvoices = new List<Invoice>();
        Invoice invoice = new Invoice();
        invoice.customerId = "1231";
        invoice.invoiceNumber = "7003";
        lstinvoices.Add(invoice);
    
        invoice = new Invoice();
        invoice.customerId = "1231";
        invoice.invoiceNumber = "7002";
        lstinvoices.Add(invoice);
        return lstinvoices;
        // so now i have two invoices for customer 1231
     }
    

и я хочу добиться этого:

       public Dictionary<string, List<Invoice>> invoiceDictionary() //this 
       is main 
       {
        Dictionary<string, List<Invoice>> invoiceDictionary = new 
        Dictionary<string, List<Invoice>>();

        string customerId = "1231";
        string invoiceNumber = "7003";

        if (invoiceDictionary.ContainsKey(customerId))
        {

            List<Invoice> lstinvoices = invoiceDictionary[customerId]; 

            var discreteInvoces = lstinvoices.Where(x => x.invoiceNumber != invoiceNumber).ToList(); // get all invoices execpt this
            invoiceDictionary[customerId] = discreteInvoces;
        }
        else
        {
            var discreteInvoces = getListInvoices().Where(x => x.invoiceNumber != invoiceNumber).ToList(); //get all the invoice except this
            invoiceDictionary.Add(customerId, discreteInvoces);
        }
        return invoiceDictionary;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...