R
R
Rostislav2018-03-30 21:10:46
MODX
Rostislav, 2018-03-30 21:10:46

How to iterate over a multidimensional array and merge elements by key?

There is such an array of addresses. The task is to display a block of information: city + address, phone number and opening hours of all establishments in this city. In this case, you need to output:
"
Moscow:
Leningradsky prospect, 26k1, 8 985 475-44-44, Daily from 12:00 to 21:00
Lenina st. 17, +7 888 888 88 88, Daily from 10:00 to 20:00
Almaty: Kurmangazy 31, corner of
Kunaev street 171, +7 771 411-14-30, Daily from 10:00 to 19:00

array(3) {
  [0]=>
  array(5) {
    ["MIGX_id"]=>
    string(1) "1"
    ["city"]=>
    string(12) "Москва"
    ["address"]=>
    string(53) "Ленинградский проспект, д 26к1"
    ["phone"]=>
    string(15) "8 985 475-44-44"
    ["workhours"]=>
    string(38) "Ежедневно с 12:00 до 21:00"
  }
  [1]=>
  array(5) {
    ["MIGX_id"]=>
    string(1) "2"
    ["city"]=>
    string(12) "Алматы"
    ["address"]=>
    string(62) "Курмангазы 31, угол ул. Кунаева д. 171"
    ["phone"]=>
    string(16) "+7 771 411-14-30"
    ["workhours"]=>
    string(38) "Ежедневно с 10:00 до 19:00"
  }
  [2]=>
  array(5) {
    ["MIGX_id"]=>
    string(1) "3"
    ["city"]=>
    string(12) "Москва"
    ["address"]=>
    string(62) "ул. Ленина 17"
    ["phone"]=>
    string(16) "+7 888 888 88 88"
    ["workhours"]=>
    string(38) "Ежедневно с 10:00 до 20:00"
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Froggyweb, 2018-03-30
@Froggyweb

{set $rows = json_decode($_modx->resource.имя tv, true)}
{foreach $rows as $row}
  <div class="item">
    <div class="title">{$row.city}</div>
    <div class="desc">{$row.address}</div>
                 .........
  </div>
{/foreach}

phenom ^)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question