3

Do functions that print arrays all use the same notation to do so? What is the reasoning for one notation over another? I noticed when using PHP print_r to display an array compiled using mysql_fetch_array, the notation of the array is as follows:

Array ([column] => value [index] = value)

For example, if this is the table:

id fname lname
1 John Smith

Then the print_r output of mysql_fetch_array will be:

Array ([id] => 1 [0] => 1 [fname] => John [1] => John [lname] => Smith [2] => Smith)

What is the reason for this notation? Specifically, why does the value repeat? Isn't that redundant?

Also, can anybody recommend a good resource for a better conceptual understanding of arrays in general? Thanks!

3 Answers 3

4

Associative arrays use strings for the index, numeric arrays use numbers.

mysql_fetch_array() creates an associative array containing numeric keys as well so that both may be used to access the array.

mysql_fetch_assoc() creates an array containing only the string indices.

mysql_fetch_row() creates an entirely numeric array, and is the fastest to execute of these functions.

Sign up to request clarification or add additional context in comments.

Comments

3

print_r & var_dump are for debugging by a developer only. Its syntax is not set in stone, and shouldn't be relied upon. The only thing that matters is that it's readable to the developer. Especially look at the source of a html document and you see a more structured formatting.

As for your second question: seems like you did a mysql_fetch_array() instead of mysql_fetch_row or mysq_fetch_assoc, see the manual pages for those functions.

As for sources to learn about arrays: have you tried the manual?

Comments

2

mysql_fetch_assoc(); will give you what you want

mysql_fetch_array allows you use

$array['id']

but either

$array[0];

mysql_fetch_row(); gives you only these numeric values and

mysql_fetch_assoc(); gives you only these string values

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.