associative array in associative array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Antoine Merieux

    associative array in associative array

    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

    #2
    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:
    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]." ";
    }
    }
    }
    >
    ?>

    Comment

    • Colin McKinnon

      #3
      Re: associative array in associative array

      Antoine Merieux wrote:
      Hi,
      I'm trying to use associative array in another associative array
      >
      <snip>
      >
      $_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.

      Comment

      Working...