|
-
Jul 14th, 2004, 12:30 PM
#1
Thread Starter
Frenzied Member
list/each [Resolved]
I'm using the following:
PHP Code:
while(list($key) = each($row))
{
echo $key . "<br>";
}
I'm just trying to get the titles of the columns in the table (I'm building a web-based query tool). However, when I echo, I get the following results:
0
Field0
1
Field1
2
Field2
etc...
I just want the field names and I don't understand why those indexes are in there. Help!? I guess I could just only echo the odd result, but that seems like a pain.
Last edited by ober0330; Jul 15th, 2004 at 02:42 PM.
-
Jul 15th, 2004, 02:14 AM
#2
I think ForEach would be better suited to your problem. Each returns an array including the value and the keys. Whereas you can use ForEach to separate each pair.
-
Jul 15th, 2004, 05:09 AM
#3
There are two ways:
Code:
foreach($array as $key => $value)
Code:
while(list($key, $value) = each($array))
I think the first is much clearer. You also don't have to reset the array if you want to go through it twice.
In both cases, you simply ignore $value if you don't want it. If you only want $value and not $key, you can write the first loop as
Code:
foreach($array as $value)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 15th, 2004, 07:25 AM
#4
Thread Starter
Frenzied Member
Helpful, but neither of you answered the question.
The code is spitting out extra keys duplicates with the same value assigned to them, but with an integer index. That's the problem I want to get rid of. I've done a work-around using a simple boolean variable to only print out the odd ones, but I want to understand why it is printing out those duplicates in the first place.
-
Jul 15th, 2004, 10:35 AM
#5
I assume you are using the mysql_fetch_array () function. This will return an array in the form:
Code:
Array (
[0] => 'value1'
['field1'] => 'value1'
[1] => 'value1'
['field2'] => 'value2'
[2] => 'value1'
['field3'] => 'value3'
)
Istead use the mysql_fetch_assoc () function which will just return the indexes with the field names.
-
Jul 15th, 2004, 02:42 PM
#6
Thread Starter
Frenzied Member
Actually, it's mssql (MS SQL 2K), but the same idea applies! You just saved me about 8 lines of code Thanks!
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
|