Results 1 to 4 of 4

Thread: multi dimensional arrays [resolved]

  1. #1

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090

    multi dimensional arrays [resolved]

    I know this might seem easy, but I have been looking and either can't find what I'm looking for or I find something that looks very complex.

    Firstly how do you define an array, I PHP is typeless, but in JavaScript I always define arrays out of habit, and I would like to keep up the habit.

    secondly, how do you make a multiD one?

    I thought it would be:
    $a = new array();
    //or new array($a);
    then:
    $a[0] = ["1","2"];
    $a[1] = ["3","4"];
    etc.

    I get errors though.
    Someone please help.
    Last edited by Acidic; Apr 2nd, 2004 at 07:11 PM.
    Have I helped you? Please Rate my posts.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    Look this over..

    PHP Code:
    <?
      //declare the array
      $arr = array();
      //declare some arrays inside of that array if you want..
      $arr[0] = array();
      $arr[1] = array();
      //add some values to the arrays
      $arr[0][0] = 'one';
      $arr[0][1] = 'two';
      $arr[1][0] = 'three';
      $arr[1][1] = 'four';
      //print out the arrays
      $br = "\n";
      echo '<pre>';
      for($i = 0; $i < count($arr); $i++){
        echo '$arr[' . $i . '] {' . $br;
        for($j = 0; $j < count($arr[$i]); $j++){
          echo '  $arr[' . $i . '][' . $j . '] = ' . $arr[$i][$j] . $br;
        }
        echo '}'  . $br . $br;
      }
      echo '</pre>';
    ?>
    Hope that helps you out a bit.
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    It makes sense actually. I don't like having to make each sub thing another array, but it makes sense, thx.
    Have I helped you? Please Rate my posts.

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Another method is:

    PHP Code:
    <?php
      
    //declare the array 
      
    $arr = array(); 

      
    //declare some arrays inside of that array if you want.. 
      
    $arr[0] = array('one''two'); 
      
    $arr[1] = array('three''four'); 
    ?>
    Or:

    PHP Code:
    <?php
      
    //declare the array 
      
    $arr = array(array('one''two'), array('three''four')); 
    ?>
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width