Connecting Tech Pros Worldwide Help | Site Map

associative array in associative array

  #1  
Old January 17th, 2007, 10:05 PM
Antoine Merieux
Guest
 
Posts: n/a
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]." ";
}
}
}

?>
  #2  
Old January 17th, 2007, 10:35 PM
Michael Cooney
Guest
 
Posts: n/a

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]." ";
}
}
}
>
?>
  #3  
Old January 18th, 2007, 09:15 PM
Colin McKinnon
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Example for an dynamically allocated associative array in C Felix H. Dahlke answers 13 June 10th, 2006 11:45 PM
What's the best way to create an associative array in C? Angus Comber answers 11 November 14th, 2005 02:56 AM
Array and Hash (Associative array) in JavaScript v.3.0 VK answers 35 August 3rd, 2005 03:45 PM
Check if key is defined in associative array JGH answers 26 July 23rd, 2005 05:20 PM