syntax for two dimensional array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • P. Henninger

    syntax for two dimensional array

    Hello,

    I have got some problems using a two dimensional array in an if expression:

    PHP does not like:

    if (table[$j]["banana"]==0)

    and says:

    Parse error: parse error, unexpected '[' in script.php on line 203



    "if ({table[$j]["banana"]==0})" was also not accepted, can anyone help `?

    I would also use the value in a two dimensional array as an index for another array:
    $array[{table[$j]["banana"]}]

    PHP does not like this, too. Can anyone help me here ?

    Cheers,

    Peter
  • Janwillem Borleffs

    #2
    Re: syntax for two dimensional array

    P. Henninger wrote:

    [color=blue]
    > I would also use the value in a two dimensional array as an index for another array:
    > $array[{table[$j]["banana"]}]
    >
    > PHP does not like this, too. Can anyone help me here ?
    >[/color]

    table should be $table


    JW


    Comment

    • P. Henninger

      #3
      Re: syntax for two dimensional array

      Janwillem Borleffs wrote:
      [color=blue]
      > P. Henninger wrote:
      >
      >[color=green]
      >> I would also use the value in a two dimensional array as an index for
      >> another array:
      >> $array[{table[$j]["banana"]}]
      >>
      >> PHP does not like this, too. Can anyone help me here ?
      >>[/color]
      >
      > table should be $table
      >
      >
      > JW
      >
      >[/color]
      Thanks, but this did not fix my problem completely.

      Using

      $array[{$table[$j]["banana"]}]

      PHP says:

      Parse error: parse error, unexpected T_CURLY_OPEN, expecting T_STRING or
      T_VARIABLE or T_NUM_STRING in script.php on line 111

      P. Henninger

      Comment

      • Pedro Graca

        #4
        Re: syntax for two dimensional array

        P. Henninger wrote:[color=blue]
        > $array[{$table[$j]["banana"]}][/color]

        $array[$table[$j]["banana"]]

        example:

        <?php
        $array = array('zero', 'one', 'two');
        $table = array();
        $table[] = array('banana'= >1, 'pineapple'=>4) ;
        $table[] = array('banana'= >2, 'pineapple'=>0) ;

        echo $table[0]['banana']; // will echo 1
        echo $array[$table[1]['pineapple']]; // will echo zero
        ?>

        --
        USENET would be a better place if everybody read: | to email me: use |
        http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
        http://www.netmeister.org/news/learn2quote2.html | header, textonly |
        http://www.expita.com/nomime.html | no attachments. |

        Comment

        • P. Henninger

          #5
          Re: syntax for two dimensional array

          Thanks !

          Pedro Graca wrote:
          [color=blue]
          > P. Henninger wrote:
          >[color=green]
          >>$array[{$table[$j]["banana"]}][/color]
          >
          >
          > $array[$table[$j]["banana"]]
          >
          > example:
          >
          > <?php
          > $array = array('zero', 'one', 'two');
          > $table = array();
          > $table[] = array('banana'= >1, 'pineapple'=>4) ;
          > $table[] = array('banana'= >2, 'pineapple'=>0) ;
          >
          > echo $table[0]['banana']; // will echo 1
          > echo $array[$table[1]['pineapple']]; // will echo zero
          > ?>
          >[/color]

          Comment

          Working...