Dobrý den,
momentálně pracuji s multidimenzionálním asociativním polem a asi nějak nerozumím tomu, jak to přesně funguje. Nevim presne jak formulovat muj problem, tak ho zkusim popsat na mem konkretnim prikladu.
Mam seznam lokalit, uzivatelu a sluzeb. Kazde sluzbe v kazde lokalite je prirazen nejaky uzivatel. Ja chci kazdemu uzivateli pro kazdou lokalitu priradit ke kazde sluzbe hodnotu 0 nebo 1 (pro nasledne zobrazeni v tabulce). Pro predstavu uvedu priklad, jak by mela tabulka vypadat.
Sluzba1 Sluzba2 Sluzba3
Uzivatel1 lokalita1 1 0 0
Uzivatel2 lokalita1 0 1 1
Uzivatel1 lokalita2 0 0 1
Uzivatel2 lokalita2 1 1 0
Kod, ktery prirazuje hodnoty jednotlivym sluzbam pro dane uzivatele dane lokality.
$localities = $this->model->getLocalities();
$services = $this->model->getServices();
foreach($localities as $locality) {
$locality['users'] = $this->model->getItWorkers();
foreach($locality['users'] as $user) {
$responsibilities = $this->model->getResponsibilities($user->id);
foreach ($services as $service) {
$user['service'][$service->name] = 0;
foreach ($responsibilities as $responsibility) {
if ($service->name == $responsibility->service) {
$user['service'][$service->name] = 1;
break;
}
}
}
}
}
A ted kde je teda problem. Kod vyse nefunguje. Pokud si dumpnu napr. var_dump($localities[0]['users']) po vykonani skriptu, klic 'services' v poli neni.
Pokud vsak kod napisu takto:
$localities = $this->model->getLocalities();
$services = $this->model->getServices();
foreach($localities as $locality) {
$locality['users'] = $this->model->getItWorkers();
foreach($locality['users'] as $user) {
$responsibilities = $this->model->getResponsibilities($user->id);
$var = $user['services'];
foreach ($services as $service) {
$var[$service->name] = 0;
foreach ($responsibilities as $responsibility) {
if ($service->name == $responsibility->service) {
$var[$service->name] = 1;
break;
}
}
}
$user['services'] = $var;
}
}
Tak kod funguje tak jak ma. Mohl by mi prosim nekdo osvetlit, jak je to mozne?