Connecting Tech Pros Worldwide Help | Site Map

associative array in associative array

Antoine Merieux
Guest
 
Posts: n/a
#1: Jan 17 '07
Hi,
I'm trying to use associative array in another associative array

to feed it's ok
to retrieve, something's wrong
can you help me
thank you

<?php
session_start();
$tabItems = array(49, 59, 69, 79);
$tabExercice = array(
'1' =$tabItems,
'45' =$tabItems);

$tab = array(
'487' =$tabExercice,
'45' =$tabExercice,
'23' =$tabExercice);

$_SESSION['caddy'] = $tab;

//print_r($tab);

// Let see what is it
foreach ($tab as $k =$v) {
// the key is id_focus
echo "Liste des exercices pour le focus".$k;
foreach ($k as $k2 =$v2) {
// the key is id_exo
echo "<br>id_exo =".$k2;
echo "<br>liste des items";
for ($i=0; $i < count($v2); $i++)
{
echo $v2[$i]." ";
}
}
}

?>
Michael Cooney
Guest
 
Posts: n/a
#2: Jan 17 '07

re: associative array in associative array


In your nested foreach, you're trying to loop over the key as opposed
to the nested array.

Change:
foreach ($k as $k2 =$v2) {
to:
foreach ($v as $k2 =$v2) {

Antoine Merieux wrote:
Quote:
Hi,
I'm trying to use associative array in another associative array
>
to feed it's ok
to retrieve, something's wrong
can you help me
thank you
>
<?php
session_start();
$tabItems = array(49, 59, 69, 79);
$tabExercice = array(
'1' =$tabItems,
'45' =$tabItems);
>
$tab = array(
'487' =$tabExercice,
'45' =$tabExercice,
'23' =$tabExercice);
>
$_SESSION['caddy'] = $tab;
>
//print_r($tab);
>
// Let see what is it
foreach ($tab as $k =$v) {
// the key is id_focus
echo "Liste des exercices pour le focus".$k;
foreach ($k as $k2 =$v2) {
// the key is id_exo
echo "<br>id_exo =".$k2;
echo "<br>liste des items";
for ($i=0; $i < count($v2); $i++)
{
echo $v2[$i]." ";
}
}
}
>
?>
Colin McKinnon
Guest
 
Posts: n/a
#3: Jan 18 '07

re: associative array in associative array


Antoine Merieux wrote:
Quote:
Hi,
I'm trying to use associative array in another associative array
>
<snip>
Quote:
>
$_SESSION['caddy'] = $tab;
In PHP4, arrays behave as if they only hold scalars in some regards (similar
to Perl) possibly in PHP5 too.

Try serializing the data before putting it in a session variable.

HTH

C.
Closed Thread