I have the following array called $zoo
Array
(
[0] => &dog&
[1] => *1*
[2] => one
[3] => &cat&
[4] => *2*
[5] => two
[6] => &mouse&
[7] => *3*
[8] => three
[9] => &dog&
[10] => *4*
[11] => four
)
and i need the following result:
Array
(
[&dog&] => Array
(
[0] => Array
(
[0] => *1*
[1] => one
)
[1] => Array
(
[0] => *4*
[1] => four
)
)
[&cat&] => Array
(
[0] => Array
(
[0] => *2*
[1] => two
)
)
[&mouse&] => Array
(
[0] => Array
(
[0] => *3*
[1] => three
)
)
)
This is what I have come up with but the problem is that for [&dog&] he gives me only the last value (namely 4, four) and not the first value (1, one)
$animals=array();
for ($i = 0; $i < count($zoo); $i++)
{
if($zoo[$i][0] === "&")
{
$name=$zoo[$i];
if (isset($name))
$animals[$name] = array($liste);
else
$animals[$name] = array($liste);
$liste="";
}
if($zoo[$i][0] !== "&")
{
$number = $zoo[$i];
$liste[] = $number;
$animals[$name] = array($liste);
}
}
print_r($animals);
this results in
Array
(
[&dog&] => Array
(
[0] => Array
(
[0] => *4*
[1] => four
)
)
[&cat&] => Array
(
[0] => Array
(
[0] => *2*
[1] => two
)
)
[&mouse&] => Array
(
[0] => Array
(
[0] => *3*
[1] => three
)
)
)
can anyone point me in the good direction?