|
-
Jan 30th, 2003, 01:45 PM
#1
Thread Starter
Addicted Member
Multidimenstional array traversal
I have a multidimensional array called $arr_results as follows:
Code:
Array
(
[0] => Array
(
[T] => Title 0
[D] => Description 0
[U] => Real URL 0
[Y] => Fake URL 0
[C] => Cost 0
[I] => Image URL 0
)
[1] => Array
(
[T] => Title 1
[D] => Description 1
[U] => Real URL 1
[Y] => Fake URL 1
[C] => Cost 1
[I] => Image URL 1
)
[2] => Array
(
[T] => Title 1
[D] => Description 1
[U] => Real URL 1
[Y] => Fake URL 1
[C] => Cost 1
[I] => Image URL 1
)
)
I want to loop through each element in the array, and for each one print out it's values for T, D, U, Y, C, I.
I'm using the following code to do it:
PHP Code:
if(count($arr_results) > 1)
{
for($counter=0; $counter<count($arr_results); $counter++)
{
print($counter + 1);
print($arr_results[$counter]["T"]);
print($arr_results[$counter]["D"]);
print($arr_results[$counter]["U"]);
print($arr_results[$counter]["Y"]);
print($arr_results[$counter]["C"]);
print($arr_results[$counter]["I"]);
}
}
This code works fine if the main array index starts at 0 and increments by 1.
My problem is that often it starts at some other number and then the whole thing falls appart.
Say for example my array looked like this, how would I do it?
Code:
Array
(
[6] => Array
(
[T] => Title 6
[D] => Description 6
[U] => Real URL 6
[Y] => Fake URL 6
[C] => Cost 6
[I] => Image URL 6
)
[8] => Array
(
[T] => Title 8
[D] => Description 8
[U] => Real URL 8
[Y] => Fake URL 8
[C] => Cost 8
[I] => Image URL 8
)
[13] => Array
(
[T] => Title 8
[D] => Description 8
[U] => Real URL 8
[Y] => Fake URL 8
[C] => Cost 8
[I] => Image URL 8
)
)
-
Jan 30th, 2003, 02:01 PM
#2
Stuck in the 80s
Like this:
Code:
while (list ($number, $vars) = each ($arr_results)) {
echo "$i<br>";
echo $vars['T'] . "<br>";
echo $vars['D'] . "<br>";
echo $vars['U'] . "<br>";
echo $vars['Y'] . "<br>";
echo $vars['C'] . "<br>";
echo $vars['I'] . "<br>";
}
-
Jan 30th, 2003, 02:34 PM
#3
Thread Starter
Addicted Member
Merci!
With the slight modification of $i to $number it works perfectly.
-
Jan 30th, 2003, 03:31 PM
#4
Stuck in the 80s
-
Jan 30th, 2003, 05:35 PM
#5
Thread Starter
Addicted Member
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
|