За последние 24 часа нас посетили 34300 программистов и 1694 робота. Сейчас ищут 685 программистов ...

Многомерный массив

Тема в разделе "PHP для новичков", создана пользователем Feonix89, 5 июл 2018.

Метки:
  1. Feonix89

    Feonix89 Новичок

    С нами с:
    30 май 2018
    Сообщения:
    113
    Симпатии:
    2
    Не могу никак понять как исправить ситуацию:
    есть код:
    PHP:
    1. $result = [];
    2.  
    3.         foreach ($sorting as $sorting_key => $sorting_value) {
    4.             if (isset($sorting_value->software_developer)
    5.                 && isset($sorting_value->licensing_option)
    6.                 && isset($sorting_value->license_type)
    7.                 && isset($sorting_value->number_of_licenses_installed))
    8.             {
    9.                 $included_in_mapping = [];  
    10.                 $included_in_mapping = ([
    11.                      'display_name' => $sorting_value->display_name,
    12.                     'product_id' => $sorting_value->product_id,
    13.                     'publisher' => $sorting_value->publisher,
    14.                     'software_developer' => $sorting_value->software_developer,
    15.                     'licensing_option' => $sorting_value->licensing_option,
    16.                     'number_of_purchased_licenses' => $sorting_value->number_of_purchased_licenses,
    17.                     'license_type' => $sorting_value->license_type,
    18.                     'number_of_licenses_installed' => $sorting_value->number_of_licenses_installed
    19.                 ]);        
    20.                
    21.                 $result = array_merge_recursive($result, $included_in_mapping);
    22.  
    23.             }
    24.  
    25.             else
    26.             {
    27.            
    28.                 $not_included_in_mapping = ([
    29.                     'display_name' => $sorting_value->display_name,
    30.                     'product_id' => $sorting_value->product_id,
    31.                     'publisher' => $sorting_value->publisher,
    32.                     'software_developer' => $sorting_value->software_developer,
    33.                     'licensing_option' => $sorting_value->licensing_option,
    34.                     'number_of_purchased_licenses' => $sorting_value->number_of_purchased_licenses,
    35.                     'license_type' => $sorting_value->license_type,
    36.                     'number_of_licenses_installed' => $sorting_value->number_of_licenses_installed
    37.                 ]);
    38.             }
    39.            
    40.         }
    41.  
    42.         dd($result);
    он выводит мне:
    Код (Text):
    1. array:8 [▼
    2.   "display_name" => array:3 [▼
    3.     0 => "Продукт_1"
    4.     1 => "1С:Предприятие"
    5.     2 => "2"
    6.   ]
    7.   "product_id" => array:3 [▼
    8.     0 => "{27735B09-9EFE-419F-A377-10AA8111C30A}"
    9.     1 => "{403EF592-953B-4794-BCEF-ECAB835C2095}"
    10.     2 => "3"
    11.   ]
    12.   "publisher" => array:3 [▼
    13.     0 => "Intel Corporation"
    14.     1 => "Intel"
    15.     2 => "3"
    16.   ]
    17.   "software_developer" => array:3 [▼
    18.     0 => "Разработчик_1"
    19.     1 => "11"
    20.     2 => "3"
    21.   ]
    22.   "licensing_option" => array:3 [▼
    23.     0 => "Вариант лицензирования"
    24.     1 => "1"
    25.     2 => "3"
    26.   ]
    27.   "number_of_purchased_licenses" => array:3 [▼
    28.     0 => null
    29.     1 => null
    30.     2 => null
    31.   ]
    32.   "license_type" => array:3 [▶]
    33.   "number_of_licenses_installed" => array:3 [▼
    34.     0 => "6"
    35.     1 => "2"
    36.     2 => "4"
    37.   ]
    38. ]
    а как получить такой вид?
    Код (Text):
    1.  
    2. Collection {#2645 ▼
    3.   #items: array:2422 [▼
    4.     0 => {#208 ▼
    5.       +"id": "1"
    6.       +"display_name": "Продукт_1"
    7.       +"product_id": "{27735B09-9EFE-419F-A377-10AA8111C30A}"
    8.       +"publisher": "Intel Corporation"
    9.       +"software_developer": "Разработчик_1"
    10.       +"name_of_software": null
    11.       +"licensing_option": "Вариант лицензирования"
    12.       +"number_of_purchased_licenses": null
    13.       +"license_type": "Тип лицензии"
    14.       +"number_of_licenses_installed": "6"
    15.     }
    16.     1 => {#2646 ▼
    17.       +"id": "2"
    18.       +"display_name": "1С:Предприятие"
    19.       +"product_id": "{403EF592-953B-4794-BCEF-ECAB835C2095}"
    20.       +"publisher": "Intel"
    21.       +"software_developer": "11"
    22.       +"name_of_software": null
    23.       +"licensing_option": "1"
    24.       +"number_of_purchased_licenses": null
    25.       +"license_type": "1"
    26.       +"number_of_licenses_installed": "2"
    27.     }
    28.     2 => {#2647 ▼
    29.       +"id": "3"
    30.       +"display_name": "2"
    31.       +"product_id": "3"
    32.       +"publisher": "3"
    33.       +"software_developer": "3"
    34.       +"name_of_software": null
    35.       +"licensing_option": "3"
    36.       +"number_of_purchased_licenses": null
    37.       +"license_type": "3"
    38.       +"number_of_licenses_installed": "4"
     
  2. Danil005

    Danil005 Активный пользователь

    С нами с:
    2 янв 2015
    Сообщения:
    528
    Симпатии:
    30
    Вынеси за пределы цикла, в начало
    --- Добавлено ---
    Замени на:
    PHP:
    1. $included_in_mapping[] = [
    2.                   'display_name' => $sorting_value->display_name,
    3.                   'product_id' => $sorting_value->product_id,
    4.                   'publisher' => $sorting_value->publisher,
    5.                   'software_developer' => $sorting_value->software_developer,
    6.                   'licensing_option' => $sorting_value->licensing_option,
    7.                   'number_of_purchased_licenses' => $sorting_value->number_of_purchased_licenses,
    8.                   'license_type' => $sorting_value->license_type,
    9.                   'number_of_licenses_installed' => $sorting_value->number_of_licenses_installed
    10.                ];  
     
    Feonix89 нравится это.
  3. Feonix89

    Feonix89 Новичок

    С нами с:
    30 май 2018
    Сообщения:
    113
    Симпатии:
    2
    @Danil005 Спасибо огромнейшее! Если можно для дурака разжевать почему так или что почитать на эту тему.
    + заменил на $result = array_merge($included_in_mapping); до этого задваивало значения
     
  4. Danil005

    Danil005 Активный пользователь

    С нами с:
    2 янв 2015
    Сообщения:
    528
    Симпатии:
    30
    Потому что ты обнулял массив в цикле, это тоже самое как $var = ""; - обнуление.
     
  5. Feonix89

    Feonix89 Новичок

    С нами с:
    30 май 2018
    Сообщения:
    113
    Симпатии:
    2
    Понятно, спасибо усвоил.