Не говорю, что ваш код не будет работать, но для меня это довольно сложно для того, что он пытается решить. Я бы разделил эту задачу на 2 этапа: 1. Преобразование данных в нужный вам формат. 2. Отобразите данные по своему усмотрению.
<?php
$data = [
"storeData"=> [
0=> [
"name"=>"STORENAME 01",
"address"=> "THIS IS ADDRESS 01",
"zip"=> "33312",
"pricing"=> [
0=> [
"price"=> 30.00,
],
],
"distance"=> 3.0000,
],
1=> [
"name"=> "STORENAME 02",
"address"=> "THIS IS ADDRESS 02",
"zip"=> "33302",
"pricing"=> [
0=> [
"price"=> 35.00,
],
],
"distance"=> 2.0000,
],
2=> [
"name"=> "STORENAME 01",
"address"=> "SAME STORE BUT AT DIFFERENT LOCATION",
"zip"=> "33301",
"pricing"=> [
0=> [
"price"=> 30.00,
],
],
"distance"=> 3.5000,
],
],
];
// 1. transform to what is needed
$stores = [];
foreach ($data['storeData'] as $store) {
if (!isset($stores[$store['name']])) {
$stores[$store['name']] = ['name' => $store['name'], 'price' => $store['pricing'][0]['price'], 'locations'=>[$store['address'] => ['address' => $store['address'], 'zip' => $store['zip']]]];
} else {
$stores[$store['name']]['locations'][$store['address']] = ['address' => $store['address'], 'zip' => $store['zip']];
}
}
var_dump($stores);
// 2. display
foreach ($stores as $store) {
echo 'store: '.$store['name'].PHP_EOL;
echo ' locations:'.PHP_EOL;
foreach ($store['locations'] as $location) {
echo ' '.$location['address'].' '.$location['zip'].PHP_EOL;
}
echo ' price: '.$store['price'].PHP_EOL;
}
Это переменная $stores
:
array(2) {
["STORENAME 01"]=>
array(3) {
["name"]=>
string(12) "STORENAME 01"
["price"]=>
float(30)
["locations"]=>
array(2) {
["THIS IS ADDRESS 01"]=>
array(2) {
["address"]=>
string(18) "THIS IS ADDRESS 01"
["zip"]=>
string(5) "33312"
}
["SAME STORE BUT AT DIFFERENT LOCATION"]=>
array(2) {
["address"]=>
string(36) "SAME STORE BUT AT DIFFERENT LOCATION"
["zip"]=>
string(5) "33301"
}
}
}
["STORENAME 02"]=>
array(3) {
["name"]=>
string(12) "STORENAME 02"
["price"]=>
float(35)
["locations"]=>
array(1) {
["THIS IS ADDRESS 02"]=>
array(2) {
["address"]=>
string(18) "THIS IS ADDRESS 02"
["zip"]=>
string(5) "33302"
}
}
}
}
Это именно то, что вы просили: каждая запись является хранилищем, каждая В магазине есть все адреса, единая цена.
Теперь его можно отображать так, как вы хотите (html, вывод на консоль и т.д. c. Я использовал вывод консоли и получил):
store: STORENAME 01
locations:
THIS IS ADDRESS 01 33312
SAME STORE BUT AT DIFFERENT LOCATION 33301
price: 30
store: STORENAME 02
locations:
THIS IS ADDRESS 02 33302
price: 35
Если вы хотите html, замените PHP_EOL
на <br />
Изменить: код без адресов местоположения в качестве ключей:
// transform to what is needed
$stores = [];
foreach ($data['storeData'] as $store) {
if (!isset($stores[$store['name']])) {
$stores[$store['name']] = ['name' => $store['name'], 'price' => $store['pricing'][0]['price'], 'locations'=> [['address' => $store['address'], 'zip' => $store['zip']]]];
} else {
$stores[$store['name']]['locations'][] = ['address' => $store['address'], 'zip' => $store['zip']];
}
}
$stores
:
array(2) {
["STORENAME 01"]=>
array(3) {
["name"]=>
string(12) "STORENAME 01"
["price"]=>
float(30)
["locations"]=>
array(2) {
[0]=>
array(2) {
["address"]=>
string(18) "THIS IS ADDRESS 01"
["zip"]=>
string(5) "33312"
}
[1]=>
array(2) {
["address"]=>
string(36) "SAME STORE BUT AT DIFFERENT LOCATION"
["zip"]=>
string(5) "33301"
}
}
}
["STORENAME 02"]=>
array(3) {
["name"]=>
string(12) "STORENAME 02"
["price"]=>
float(35)
["locations"]=>
array(1) {
[0]=>
array(2) {
["address"]=>
string(18) "THIS IS ADDRESS 02"
["zip"]=>
string(5) "33302"
}
}
}
}