<?php
$importedDataRaw = [
[17, 'Новосибирск', '', 'Бонапарт', 'г. Новосибирск, ул. Никитина, 20'],
['', '', '', 'Сибинтеркамень', 'г. Новосибирск, Ползунова, 1 к21, 2 этаж'],
['', '', '', 'Алексстрой', 'г. Новосибирск, ул. Светлановская, д. 52.'],
[50, 'г. Мариуполь', '', '', 'ул. Митрополитская 105'],
['', '', '', '', 'Торговая ,45'],
['', '', '', '', 'Амурская 3А'],
['', '', '', '', 'ул.Фонтанная 73'],
[51, 'Макеевка', '', '', 'пр. Генерала Данилова, 71'],
[52, 'ДНР г. Макеевка', '', '', 'ул. Московская 1а'],
['', '', '', '', 'ул. 259-летя Донбаса 74']
];
$fields = ['MIGX_id', 'title_city', 'text_city', 'name', 'address'];
$importedData = array_map(fn($row) => array_combine($fields, $row), $importedDataRaw);
$result = [];
$cityGroups = [];
$lastCityId = null;
foreach ($importedData as $item) {
if (!empty(trim($item['MIGX_id']))) {
// Если есть новый город, запоминаем его
$lastCityId = $item['MIGX_id'];
$cityGroups[$lastCityId] = [
'MIGX_id' => $item['MIGX_id'],
'title_city' => $item['title_city'],
'text_city' => $item['text_city'],
'adreses' => []
];
}
if (!empty(trim($item['address']))) {
// Добавляем адрес к последнему найденному городу
if ($lastCityId !== null) {
$cityGroups[$lastCityId]['adreses'][] = [
'MIGX_id' => count($cityGroups[$lastCityId]['adreses']) + 1,
'name' => $item['name'],
'adress' => $item['address']
];
}
}
}
// Преобразуем массив городов в финальный результат
$result = array_values($cityGroups);
print_r($result);