I'm a newbie in PHP so please guide me to better practice. Basically, I have this array $content, and I would like to change it according to some keys existence. Here's my array :
Array
(
[civilite] => 3
[first_name] => first_name
[last_name] => last_name
[telephone] => xxxxxxxxxxx
[fax] => xxxxxxxxx
[telephone_portable] => xxxxxxxxxxxx
[mail] => [email protected]
[naissance] => xx/xx/xxx
[profession] => xxxxxxxxx
[activite] => 1
[cb_moto] => 1
[adresse_moto] => moto 1
[cp_moto] => xxxxxx 1
[ville_moto] => xxxxxxxxx
[moto_permis_1] => A1
[moto_permis_2] => B1
[moto_permis_3] => A1
[moto_marque] => xxxx
[cb_auto] => 1
[adresse_auto] => auto 2
[cp_auto] => xxxxxxxx 2
[ville_auto] => xxxxxxx
[auto_permis_1] => A 2
[auto_permis_2] => B 2
[auto_permis_3] => A2
[auto_marque] => xxxx
);
And here's what I want to change it to :
Array
(
[general] => Array
(
[civilite] => 3
[first_name] => first_name
[last_name] => last_name
[telephone] => xxxxxxxxxxx
[fax] => xxxxxxxxx
[telephone_portable] => xxxxxxxxxxxx
[mail] => [email protected]
[naissance] => xx/xx/xxx
[profession] => xxxxxxxxx
[activite] => 1
)
[moto] => Array
(
[cb_moto] => 1
[adresse_moto] => moto 1
[cp_moto] => xxxxxx 1
[ville_moto] => xxxxxxxxx
[moto_permis_1] => A1
[moto_permis_2] => B1
[moto_permis_3] => A1
[moto_marque] => xxxx
)
[auto] => Array
(
[cb_auto] => 1
[adresse_auto] => auto 2
[cp_auto] => xxxxxxxx 2
[ville_auto] => xxxxxxx
[auto_permis_1] => A 2
[auto_permis_2] => B 2
[auto_permis_3] => A2
[auto_marque] => xxxx
)
);
The idea is if the key cb_moto exists create a sub-array that has the key moto in it until the appearance of the key cb_auto and create another sub-array that has the key auto and so on.
Here's what I've tried so far :
foreach ($content as $key => $value) {
$content ['general'][] = $value;
if(isset($_content['cb_moto'])){
$content ['moto'][] = $value;
}
}
Any help with this? Much appreciated.