How to print elemnets of N-dimensional array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • opt_inf_env@yahoo.com

    How to print elemnets of N-dimensional array?

    Hello,

    What is a common way to print elements of a multidimensiona l array? I
    treid to do it in the straightforward way:
    $input_list[0][0] = "Something" ;
    print "$input_lis t[0][0]";
    And it does not work.

    The solution that I found is to use intermediate variable:
    $array = $input_list[0];
    print "$array[0]";
    printArray( $output_list[0] );

    Are there any more elegant (or just common) ways to do it?

  • Janwillem Borleffs

    #2
    Re: How to print elemnets of N-dimensional array?

    opt_inf_env@yah oo.com wrote:[color=blue]
    > What is a common way to print elements of a multidimensiona l array? I
    > treid to do it in the straightforward way:
    > $input_list[0][0] = "Something" ;
    > print "$input_lis t[0][0]";
    > And it does not work.
    >[/color]

    Either one of the following should work:

    print $input_list[0][0];
    print "{$input_li st[0][0]}";

    Notice the braces used in the second example.


    JW



    Comment

    • Senator Jay Billington Bulworth

      #3
      Re: How to print elemnets of N-dimensional array?

      opt_inf_env@yah oo.com wrote in news:1109517346 .824559.113520
      @f14g2000cwb.go oglegroups.com:
      [color=blue]
      > Hello,
      >
      > What is a common way to print elements of a multidimensiona l array? I
      > treid to do it in the straightforward way:
      > $input_list[0][0] = "Something" ;
      > print "$input_lis t[0][0]";
      > And it does not work.[/color]

      You're close. PHP automatically expands variables inside of quotation
      marks. Thus, when you write

      print "$input_lis t[0][0]";

      PHP sees this as a string because it's in quotation marks, and attempts
      to replace the text "$input_lis t" with the value of the $input_list
      variable. In this case, $input_list is an array, and trying to print or
      echo an array by name results in the string "Array."

      To solve this problem, just remove the quotation marks:

      print $input_list[0][0];

      hth


      --
      Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fu ng.arg');
      --------------------------|---------------------------------
      <http://www.phplabs.com/> | PHP scripts, webmaster resources

      Comment

      • Jan Pieter Kunst

        #4
        Re: How to print elemnets of N-dimensional array?

        Senator Jay Billington Bulworth wrote:[color=blue]
        > opt_inf_env@yah oo.com wrote in news:1109517346 .824559.113520
        > @f14g2000cwb.go oglegroups.com:
        >
        >[color=green]
        >>Hello,
        >>
        >>What is a common way to print elements of a multidimensiona l array? I
        >>treid to do it in the straightforward way:
        >>$input_list[0][0] = "Something" ;
        >>print "$input_lis t[0][0]";
        >>And it does not work.[/color]
        >
        >
        > You're close. PHP automatically expands variables inside of quotation
        > marks. Thus, when you write
        >
        > print "$input_lis t[0][0]";
        >
        > PHP sees this as a string because it's in quotation marks, and attempts
        > to replace the text "$input_lis t" with the value of the $input_list
        > variable. In this case, $input_list is an array, and trying to print or
        > echo an array by name results in the string "Array."
        >
        > To solve this problem, just remove the quotation marks:
        >
        > print $input_list[0][0];[/color]


        Or, if you need to print the value embedded in a string, do this:

        print "{$input_li st[0][0]}";

        HTH,
        JP

        --
        Sorry, <devnull@cauce. org> is a spam trap.
        Real e-mail address unavailable. 5000+ spams per month.

        Comment

        Working...