|
-
Apr 2nd, 2004, 03:53 PM
#1
Thread Starter
Frenzied Member
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. 
-
Apr 2nd, 2004, 06:53 PM
#2
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.
-
Apr 2nd, 2004, 07:11 PM
#3
Thread Starter
Frenzied Member
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. 
-
Apr 4th, 2004, 07:55 AM
#4
Stuck in the 80s
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'));
?>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|