Мне нужно создать довольно большой вложенный объект, который представляет гостевые данные для парка развлечений. По сути, в базе данных есть 4 категории полов: мужские, женские, недвоичные и без ответа. Существует также 8 возрастных групп, обозначенных буквами от А до G. Наконец, в каждой возрастной группе должна быть пара ключевых значений (например, фуд-корт) с количеством гостей в этой зоне. Ниже вы заметите, что массив genders жестко запрограммирован и содержит также категории «отвечено» и «все». Мне также нужно иметь сумму для каждой группы, чтобы я мог рассчитать значения для этих двух категорий. Я надеюсь, что это имеет смысл.
В конечном итоге мне нужно создать объект, который выглядит следующим образом:
"data": {
"gender_male": {
"agegroup_A": {
"Location Not Known": 5,
"Main Gate Area": 7,
"Food & Beverage Area": 5,
"Live! Lagoon": 7,
"Reply Racers / Drop Slides": 4,
"Private Domain": 8,
"Thrill Zone": 7,
"Cabana Suites": 8,
"Island": 4,
"Chat Creek": 9,
"Family Slides": 4,
"Virtual Village": 1,
"Unknown Location": 6,
"agegroup_count": 75
},
"agegroup_B": {
"Location Not Known": 8,
"Main Gate Area": 8,
"Food & Beverage Area": 2,
"Live! Lagoon": 6,
"Reply Racers / Drop Slides": 2,
"Private Domain": 8,
"Thrill Zone": 7,
"Cabana Suites": 3,
"Island": 3,
"Chat Creek": 5,
"Family Slides": 5,
"Virtual Village": 4,
"Unknown Location": 7,
"agegroup_count": 68
},
"agegroup_C": {
"Location Not Known": 6,
"Main Gate Area": 6,
"Food & Beverage Area": 3,
"Live! Lagoon": 1,
"Reply Racers / Drop Slides": 8,
"Private Domain": 3,
"Thrill Zone": 3,
"Cabana Suites": 4,
"Island": 7,
"Chat Creek": 3,
"Family Slides": 7,
"Virtual Village": 5,
"Unknown Location": 6,
"agegroup_count": 62
},
"agegroup_D": {
"Location Not Known": 7,
"Main Gate Area": 2,
"Food & Beverage Area": 2,
"Live! Lagoon": 6,
"Reply Racers / Drop Slides": 3,
"Private Domain": 6,
"Thrill Zone": 5,
"Cabana Suites": 8,
"Island": 4,
"Chat Creek": 9,
"Family Slides": 5,
"Virtual Village": 4,
"Unknown Location": 2,
"agegroup_count": 63
},
"agegroup_E": {
"Location Not Known": 7,
"Main Gate Area": 1,
"Food & Beverage Area": 7,
"Live! Lagoon": 9,
"Reply Racers / Drop Slides": 5,
"Private Domain": 5,
"Thrill Zone": 5,
"Cabana Suites": 8,
"Island": 2,
"Chat Creek": 7,
"Family Slides": 9,
"Virtual Village": 8,
"Unknown Location": 4,
"agegroup_count": 77
},
"agegroup_F": {
"Location Not Known": 8,
"Main Gate Area": 8,
"Food & Beverage Area": 6,
"Live! Lagoon": 4,
"Reply Racers / Drop Slides": 4,
"Private Domain": 5,
"Thrill Zone": 6,
"Cabana Suites": 5,
"Island": 7,
"Chat Creek": 6,
"Family Slides": 9,
"Virtual Village": 2,
"Unknown Location": 8,
"agegroup_count": 78
},
"agegroup_G": {
"Location Not Known": 4,
"Main Gate Area": 6,
"Food & Beverage Area": 3,
"Live! Lagoon": 6,
"Reply Racers / Drop Slides": 9,
"Private Domain": 5,
"Thrill Zone": 2,
"Cabana Suites": 9,
"Island": 1,
"Chat Creek": 9,
"Family Slides": 7,
"Virtual Village": 7,
"Unknown Location": 5,
"agegroup_count": 73
},
"gender_count": 496
},
"gender_female": {
"agegroup_A": {
"Location Not Known": 9,
"Main Gate Area": 7,
"Food & Beverage Area": 7,
"Live! Lagoon": 2,
"Reply Racers / Drop Slides": 6,
"Private Domain": 3,
"Thrill Zone": 2,
"Cabana Suites": 7,
"Island": 1,
"Chat Creek": 2,
"Family Slides": 5,
"Virtual Village": 2,
"Unknown Location": 2,
"agegroup_count": 55
},
"agegroup_B": {
"Location Not Known": 9,
"Main Gate Area": 7,
"Food & Beverage Area": 3,
"Live! Lagoon": 9,
"Reply Racers / Drop Slides": 2,
"Private Domain": 4,
"Thrill Zone": 6,
"Cabana Suites": 5,
"Island": 1,
"Chat Creek": 3,
"Family Slides": 4,
"Virtual Village": 7,
"Unknown Location": 2,
"agegroup_count": 62
},
"agegroup_C": {
"Location Not Known": 9,
"Main Gate Area": 6,
"Food & Beverage Area": 4,
"Live! Lagoon": 5,
"Reply Racers / Drop Slides": 7,
"Private Domain": 9,
"Thrill Zone": 5,
"Cabana Suites": 1,
"Island": 9,
"Chat Creek": 2,
"Family Slides": 9,
"Virtual Village": 9,
"Unknown Location": 4,
"agegroup_count": 79
},
"agegroup_D": {
"Location Not Known": 9,
"Main Gate Area": 3,
"Food & Beverage Area": 4,
"Live! Lagoon": 9,
"Reply Racers / Drop Slides": 3,
"Private Domain": 5,
"Thrill Zone": 9,
"Cabana Suites": 9,
"Island": 3,
"Chat Creek": 8,
"Family Slides": 8,
"Virtual Village": 9,
"Unknown Location": 1,
"agegroup_count": 80
},
"agegroup_E": {
"Location Not Known": 4,
"Main Gate Area": 5,
"Food & Beverage Area": 3,
"Live! Lagoon": 6,
"Reply Racers / Drop Slides": 7,
"Private Domain": 6,
"Thrill Zone": 8,
"Cabana Suites": 5,
"Island": 1,
"Chat Creek": 9,
"Family Slides": 1,
"Virtual Village": 1,
"Unknown Location": 5,
"agegroup_count": 61
},
"agegroup_F": {
"Location Not Known": 7,
"Main Gate Area": 1,
"Food & Beverage Area": 2,
"Live! Lagoon": 5,
"Reply Racers / Drop Slides": 2,
"Private Domain": 4,
"Thrill Zone": 9,
"Cabana Suites": 7,
"Island": 3,
"Chat Creek": 7,
"Family Slides": 6,
"Virtual Village": 2,
"Unknown Location": 6,
"agegroup_count": 61
},
"agegroup_G": {
"Location Not Known": 9,
"Main Gate Area": 3,
"Food & Beverage Area": 6,
"Live! Lagoon": 8,
"Reply Racers / Drop Slides": 2,
"Private Domain": 4,
"Thrill Zone": 9,
"Cabana Suites": 3,
"Island": 5,
"Chat Creek": 3,
"Family Slides": 1,
"Virtual Village": 3,
"Unknown Location": 2,
"agegroup_count": 58
},
"gender_count": 456
},
"gender_nonbinary": {
"agegroup_A": {
"Location Not Known": 5,
"Main Gate Area": 3,
"Food & Beverage Area": 8,
"Live! Lagoon": 4,
"Reply Racers / Drop Slides": 5,
"Private Domain": 9,
"Thrill Zone": 4,
"Cabana Suites": 3,
"Island": 4,
"Chat Creek": 8,
"Family Slides": 4,
"Virtual Village": 2,
"Unknown Location": 9,
"agegroup_count": 68
},
"agegroup_B": {
"Location Not Known": 3,
"Main Gate Area": 2,
"Food & Beverage Area": 8,
"Live! Lagoon": 9,
"Reply Racers / Drop Slides": 6,
"Private Domain": 8,
"Thrill Zone": 5,
"Cabana Suites": 8,
"Island": 4,
"Chat Creek": 8,
"Family Slides": 2,
"Virtual Village": 1,
"Unknown Location": 3,
"agegroup_count": 67
},
"agegroup_C": {
"Location Not Known": 4,
"Main Gate Area": 9,
"Food & Beverage Area": 1,
"Live! Lagoon": 7,
"Reply Racers / Drop Slides": 3,
"Private Domain": 8,
"Thrill Zone": 2,
"Cabana Suites": 2,
"Island": 5,
"Chat Creek": 3,
"Family Slides": 5,
"Virtual Village": 1,
"Unknown Location": 6,
"agegroup_count": 56
},
"agegroup_D": {
"Location Not Known": 7,
"Main Gate Area": 7,
"Food & Beverage Area": 4,
"Live! Lagoon": 5,
"Reply Racers / Drop Slides": 5,
"Private Domain": 8,
"Thrill Zone": 2,
"Cabana Suites": 7,
"Island": 5,
"Chat Creek": 3,
"Family Slides": 6,
"Virtual Village": 7,
"Unknown Location": 2,
"agegroup_count": 68
},
"agegroup_E": {
"Location Not Known": 8,
"Main Gate Area": 1,
"Food & Beverage Area": 4,
"Live! Lagoon": 2,
"Reply Racers / Drop Slides": 4,
"Private Domain": 2,
"Thrill Zone": 8,
"Cabana Suites": 8,
"Island": 9,
"Chat Creek": 1,
"Family Slides": 8,
"Virtual Village": 2,
"Unknown Location": 9,
"agegroup_count": 66
},
"agegroup_F": {
"Location Not Known": 4,
"Main Gate Area": 4,
"Food & Beverage Area": 1,
"Live! Lagoon": 5,
"Reply Racers / Drop Slides": 5,
"Private Domain": 4,
"Thrill Zone": 5,
"Cabana Suites": 2,
"Island": 6,
"Chat Creek": 3,
"Family Slides": 3,
"Virtual Village": 1,
"Unknown Location": 9,
"agegroup_count": 52
},
"agegroup_G": {
"Location Not Known": 1,
"Main Gate Area": 7,
"Food & Beverage Area": 2,
"Live! Lagoon": 9,
"Reply Racers / Drop Slides": 8,
"Private Domain": 7,
"Thrill Zone": 4,
"Cabana Suites": 3,
"Island": 2,
"Chat Creek": 4,
"Family Slides": 1,
"Virtual Village": 7,
"Unknown Location": 6,
"agegroup_count": 61
},
"gender_count": 438
},
"gender_unanswered": {
"agegroup_A": {
"Location Not Known": 1,
"Main Gate Area": 3,
"Food & Beverage Area": 9,
"Live! Lagoon": 2,
"Reply Racers / Drop Slides": 4,
"Private Domain": 1,
"Thrill Zone": 9,
"Cabana Suites": 9,
"Island": 3,
"Chat Creek": 6,
"Family Slides": 9,
"Virtual Village": 7,
"Unknown Location": 1,
"agegroup_count": 64
},
"agegroup_B": {
"Location Not Known": 5,
"Main Gate Area": 8,
"Food & Beverage Area": 3,
"Live! Lagoon": 6,
"Reply Racers / Drop Slides": 8,
"Private Domain": 4,
"Thrill Zone": 7,
"Cabana Suites": 6,
"Island": 8,
"Chat Creek": 3,
"Family Slides": 9,
"Virtual Village": 6,
"Unknown Location": 6,
"agegroup_count": 79
},
"agegroup_C": {
"Location Not Known": 4,
"Main Gate Area": 5,
"Food & Beverage Area": 3,
"Live! Lagoon": 6,
"Reply Racers / Drop Slides": 2,
"Private Domain": 5,
"Thrill Zone": 5,
"Cabana Suites": 1,
"Island": 1,
"Chat Creek": 7,
"Family Slides": 4,
"Virtual Village": 5,
"Unknown Location": 9,
"agegroup_count": 57
},
"agegroup_D": {
"Location Not Known": 9,
"Main Gate Area": 9,
"Food & Beverage Area": 9,
"Live! Lagoon": 2,
"Reply Racers / Drop Slides": 2,
"Private Domain": 1,
"Thrill Zone": 6,
"Cabana Suites": 8,
"Island": 5,
"Chat Creek": 9,
"Family Slides": 5,
"Virtual Village": 9,
"Unknown Location": 4,
"agegroup_count": 78
},
"agegroup_E": {
"Location Not Known": 2,
"Main Gate Area": 2,
"Food & Beverage Area": 3,
"Live! Lagoon": 8,
"Reply Racers / Drop Slides": 4,
"Private Domain": 8,
"Thrill Zone": 8,
"Cabana Suites": 1,
"Island": 1,
"Chat Creek": 6,
"Family Slides": 8,
"Virtual Village": 4,
"Unknown Location": 3,
"agegroup_count": 58
},
"agegroup_F": {
"Location Not Known": 1,
"Main Gate Area": 6,
"Food & Beverage Area": 6,
"Live! Lagoon": 6,
"Reply Racers / Drop Slides": 2,
"Private Domain": 5,
"Thrill Zone": 5,
"Cabana Suites": 5,
"Island": 5,
"Chat Creek": 5,
"Family Slides": 5,
"Virtual Village": 4,
"Unknown Location": 7,
"agegroup_count": 62
},
"agegroup_G": {
"Location Not Known": 4,
"Main Gate Area": 9,
"Food & Beverage Area": 1,
"Live! Lagoon": 1,
"Reply Racers / Drop Slides": 4,
"Private Domain": 1,
"Thrill Zone": 6,
"Cabana Suites": 5,
"Island": 4,
"Chat Creek": 5,
"Family Slides": 1,
"Virtual Village": 4,
"Unknown Location": 6,
"agegroup_count": 51
},
"gender_count": 449
},
"gender_answered": {
"agegroup_A": {
"Location Not Known": 4,
"Main Gate Area": 3,
"Food & Beverage Area": 2,
"Live! Lagoon": 7,
"Reply Racers / Drop Slides": 6,
"Private Domain": 8,
"Thrill Zone": 5,
"Cabana Suites": 4,
"Island": 3,
"Chat Creek": 1,
"Family Slides": 1,
"Virtual Village": 6,
"Unknown Location": 8,
"agegroup_count": 58
},
"agegroup_B": {
"Location Not Known": 5,
"Main Gate Area": 8,
"Food & Beverage Area": 6,
"Live! Lagoon": 3,
"Reply Racers / Drop Slides": 5,
"Private Domain": 7,
"Thrill Zone": 3,
"Cabana Suites": 4,
"Island": 7,
"Chat Creek": 2,
"Family Slides": 6,
"Virtual Village": 3,
"Unknown Location": 7,
"agegroup_count": 66
},
"agegroup_C": {
"Location Not Known": 9,
"Main Gate Area": 1,
"Food & Beverage Area": 8,
"Live! Lagoon": 8,
"Reply Racers / Drop Slides": 7,
"Private Domain": 8,
"Thrill Zone": 3,
"Cabana Suites": 8,
"Island": 2,
"Chat Creek": 9,
"Family Slides": 5,
"Virtual Village": 1,
"Unknown Location": 4,
"agegroup_count": 73
},
"agegroup_D": {
"Location Not Known": 5,
"Main Gate Area": 4,
"Food & Beverage Area": 2,
"Live! Lagoon": 4,
"Reply Racers / Drop Slides": 8,
"Private Domain": 4,
"Thrill Zone": 7,
"Cabana Suites": 4,
"Island": 4,
"Chat Creek": 5,
"Family Slides": 7,
"Virtual Village": 4,
"Unknown Location": 2,
"agegroup_count": 60
},
"agegroup_E": {
"Location Not Known": 8,
"Main Gate Area": 6,
"Food & Beverage Area": 1,
"Live! Lagoon": 4,
"Reply Racers / Drop Slides": 1,
"Private Domain": 3,
"Thrill Zone": 6,
"Cabana Suites": 8,
"Island": 2,
"Chat Creek": 8,
"Family Slides": 3,
"Virtual Village": 3,
"Unknown Location": 3,
"agegroup_count": 56
},
"agegroup_F": {
"Location Not Known": 7,
"Main Gate Area": 8,
"Food & Beverage Area": 7,
"Live! Lagoon": 8,
"Reply Racers / Drop Slides": 9,
"Private Domain": 2,
"Thrill Zone": 9,
"Cabana Suites": 2,
"Island": 8,
"Chat Creek": 3,
"Family Slides": 1,
"Virtual Village": 7,
"Unknown Location": 6,
"agegroup_count": 77
},
"agegroup_G": {
"Location Not Known": 2,
"Main Gate Area": 5,
"Food & Beverage Area": 3,
"Live! Lagoon": 3,
"Reply Racers / Drop Slides": 8,
"Private Domain": 2,
"Thrill Zone": 7,
"Cabana Suites": 4,
"Island": 7,
"Chat Creek": 5,
"Family Slides": 1,
"Virtual Village": 7,
"Unknown Location": 5,
"agegroup_count": 59
},
"gender_count": 1390
},
"gender_all": {
"agegroup_A": {
"Location Not Known": 1,
"Main Gate Area": 9,
"Food & Beverage Area": 3,
"Live! Lagoon": 9,
"Reply Racers / Drop Slides": 7,
"Private Domain": 7,
"Thrill Zone": 5,
"Cabana Suites": 7,
"Island": 1,
"Chat Creek": 6,
"Family Slides": 6,
"Virtual Village": 1,
"Unknown Location": 9,
"agegroup_count": 71
},
"agegroup_B": {
"Location Not Known": 2,
"Main Gate Area": 2,
"Food & Beverage Area": 2,
"Live! Lagoon": 3,
"Reply Racers / Drop Slides": 8,
"Private Domain": 6,
"Thrill Zone": 9,
"Cabana Suites": 5,
"Island": 5,
"Chat Creek": 3,
"Family Slides": 5,
"Virtual Village": 9,
"Unknown Location": 5,
"agegroup_count": 64
},
"agegroup_C": {
"Location Not Known": 3,
"Main Gate Area": 2,
"Food & Beverage Area": 6,
"Live! Lagoon": 7,
"Reply Racers / Drop Slides": 8,
"Private Domain": 3,
"Thrill Zone": 8,
"Cabana Suites": 9,
"Island": 5,
"Chat Creek": 9,
"Family Slides": 9,
"Virtual Village": 7,
"Unknown Location": 5,
"agegroup_count": 81
},
"agegroup_D": {
"Location Not Known": 8,
"Main Gate Area": 8,
"Food & Beverage Area": 7,
"Live! Lagoon": 4,
"Reply Racers / Drop Slides": 5,
"Private Domain": 4,
"Thrill Zone": 9,
"Cabana Suites": 4,
"Island": 1,
"Chat Creek": 8,
"Family Slides": 5,
"Virtual Village": 4,
"Unknown Location": 9,
"agegroup_count": 76
},
"agegroup_E": {
"Location Not Known": 6,
"Main Gate Area": 9,
"Food & Beverage Area": 9,
"Live! Lagoon": 2,
"Reply Racers / Drop Slides": 9,
"Private Domain": 9,
"Thrill Zone": 9,
"Cabana Suites": 5,
"Island": 1,
"Chat Creek": 5,
"Family Slides": 8,
"Virtual Village": 6,
"Unknown Location": 7,
"agegroup_count": 85
},
"agegroup_F": {
"Location Not Known": 5,
"Main Gate Area": 7,
"Food & Beverage Area": 4,
"Live! Lagoon": 4,
"Reply Racers / Drop Slides": 8,
"Private Domain": 1,
"Thrill Zone": 2,
"Cabana Suites": 3,
"Island": 9,
"Chat Creek": 7,
"Family Slides": 8,
"Virtual Village": 9,
"Unknown Location": 7,
"agegroup_count": 74
},
"agegroup_G": {
"Location Not Known": 8,
"Main Gate Area": 5,
"Food & Beverage Area": 8,
"Live! Lagoon": 1,
"Reply Racers / Drop Slides": 7,
"Private Domain": 5,
"Thrill Zone": 8,
"Cabana Suites": 3,
"Island": 8,
"Chat Creek": 4,
"Family Slides": 1,
"Virtual Village": 7,
"Unknown Location": 8,
"agegroup_count": 73
},
"gender_count": 1839
}
}
Мне нужно отформатировать его с помощью mysqli_query, который я создал в своем скрипте PHP. Причина, по которой вышеупомянутый объект является недопустимым, заключается в том, что в настоящее время он использует жестко закодированные данные. Данные выглядят так:
Мой код пока:
$locationdata_query = 'SELECT gp.gender, gp.agegroup, lz.zonename, lz.loczoneid as zoneid, count(*) as count
FROM vantage_local.guests g
JOIN vantage_local.guests_personal gp
ON g.gid = gp.gid
JOIN vantage_local.location_zones lz
ON g.loczoneid = lz.loczoneid
GROUP BY gp.gender, gp.agegroup, lz.zonename, lz.loczoneid
ORDER BY gp.gender DESC, gp.agegroup, lz.zonename';
foreach(mysqli_query($sqlhandle, $locationdata_query) as $data) {
$counts[] = array($data['zonename'] => $data['count']);
}
$age = [];
array_push($age, '-');
foreach (mysqli_query($sqlhandle, 'SELECT DISTINCT age_group FROM vantage_local.age_groups') as $agegroup) {
$age[] = $agegroup['age_group'];
}
foreach($genders as $gender) {
$gender_counter = 0;
foreach($age as $agegroup) {
$agegroup_counter = 0;
foreach($counts as $zonecount) {
foreach($zonecount as $zone => $count) {
}
$agegroup_val = $locationdata['gender_'.$gender]['agegroup_'.$agegroup][$zone] = (int) $count;
$agegroup_counter += $agegroup_val;
}
$agegroup_totals = $locationdata['gender_'.$gender]['agegroup_'.$agegroup]['agegroup_count'] = $agegroup_counter;
$gender_counter += $agegroup_totals;
}
// get total answered (male, female, and nonbinary)
if ($gender === 'answered') {
$gender_counter = $locationdata['gender_female']['gender_count']
+ $locationdata['gender_male']['gender_count']
+ $locationdata['gender_nonbinary']['gender_count'];
}
// get total
if ($gender === 'all') {
$gender_counter = $locationdata['gender_answered']['gender_count']
+ $locationdata['gender_unanswered']['gender_count'];
}
$locationdata['gender_'.$gender]['gender_count'] = $gender_counter;
}
Исходный код с жестко закодированными данными выглядит аналогично но имеет одно вложенное меньше l oop, а устанавливаемое значение - это просто случайное число от 1 до 9. По какой-то причине я не могу получить соответствующие значения для сопоставления с правильными группами / категориями. Все они имеют одинаковые 13 значений для каждой возрастной группы, например:
"Family Slides": 4,
"Food & Beverage Area": 1,
"Island": 1,
"Location Not Known": 1,
"Main Gate Area": 2,
"Reply Racers / Drop Slides": 1,
"Thrill Zone": 5,
"Live! Lagoon": 1,
"Private Domain": 3,
"Virtual Village": 1,
"agegroup_count": 30246