| Difference between general array and PHP Arrays [message #1415] |
Sat, 28 July 2007 09:22  |
|
Hi Friends..
I am sai suman.. I am preparing for ZCE.. I need a help from u people.. Can anyone note me the differences between general arrays and Associative array, enumerated arrays..
Thanks in advance frnds..
Sai Suman Gubba | +91 9849894188
Technical Associate | Cytrion Technologies
|
|
|
| Re: Difference between general array and PHP Arrays [message #1416 is a reply to message #1415 ] |
Sat, 28 July 2007 16:12   |
j.rf Messages: 139 Registered: October 2006 Location: The Netherlands |
Member |
|
|
Hi Sai,
I'll put in my two pennies - no doubt others will expand on this:
Basically an array is an array is an array, i.e. on the whole all types of arrays will function the same (or nearly the same).
To refer to your question:
- a 'general' array (no keys) will result in an enumerated array with keys starting at 0 and going up one at the time
- an 'enumerated' array will have numerical keys, but may skip numbers and numbers may be added in any order (mind: this also implies that you can / could easily overwrite an assigned value if you don't keep track of which numerical keys have been assigned already - then again, you may intend to do that).
- an 'associative' array will have string keys, but the values can also be called by number. (mind: same warning about overwriting values goes here)
There are several methods of creating arrays:
<?php // Create and fill in array in one go with no keys, just values
$myarray = array(
'string a',
'string b',
'string c',
);
// .. with numerical keys and values (enumerated array)
$myarray = array(
[0] => 'string a',
[2] => 'string b',
[40] => 'string c',
[1] => 'string d',
);
// .. with string keys and values (associative array)
$myarray = array(
['zero'] => 'string a',
['one'] => 'string b',
['two'] => 'string c',
['forty'] => 'string d',
);
// Alternative method:
//Create array ..
$myarray = array();
// .. and add values to the next unused numerical index
$myarray[] = 'string a';
$myarray[] = 'string b';
$myarray[] = 'string c';
// Create array ..
$myarray = array();
// .. and add values using associative keys:
$myarray['zero'] = 'string a';
$myarray['one'] = 'string b';
$myarray['two'] = 'string c';
// and you can also mix the key types:
$myarray = array();
$myarray[] = 'string a';
$myarray['zero'] = 'string a';
$myarray[] = 'string b';
$myarray['one'] = 'string b';
$myarray[] = 'string c';
$myarray['two'] = 'string c';
$myarray[40] = 'string c';
//and of course you can also have nested arrays:
$myarray = array();
$myarray[] = 'string a';
$myarray['zero'] = 'string a';
$myarray[] = array( 'string b' );
$myarray['one'][] = 'string b';
$myarray['one'][] = 'string c';
//Hint: use the below code for each of the above examples to have a good look at the resulting arrays and to see the differences
print_r( $myarray ); ?>
Basically when you decide how you define your array, you have to take into consideration how you want to use it.
* Do you need to be able to look up a specific value ? or will you just loop through the array ?
* Does the order of the array matter ?
* etc etc
Values in an enumerated array, can only be called by their number.
Values in an associative array can be called both by their number as well as by their associative key.
<?php $myarray = array(
[0] => 'string a',
[1] => 'string b',
[2] => 'string c',
);
print $myarray[1]; // string b
$myarray = array(
['zero'] => 'string a',
['one'] => 'string b',
['two'] => 'string c',
);
print $myarray['one']; // string b
print $myarray[1]; // string b ?>
Two of the most used methods of 'walking' an array are for and foreach.
<?php $arraycount = count( $myarray );
for( $i = 0; $i < $arraycount; $i++ ) {
print 'key is: ' . $i . ' - value is ' . $myarray[$i] . '<br />';
} ?>
The for loop will walk through the array using the numerical order (i.e. not necessarily the order in which the values where added to the array if you used given numerical keys).
Mind: if you assigned numerical keys and have numerical gaps in your key range - i.e. 0, 2, 4, 10 - the array count will be 4 and your print when using the above example will stop after the first two items. (As when $i = 4, $i < $arraycount (=4) will no longer be valid)
<?php foreach( $myarray as $key => $value ) {
print 'key is: ' . $key . ' - value is ' . $value . '<br />';
} ?>
The foreach loop will walk through the array in the order the values where added to the array, no matter what. The keys by which the array values where defined will be used, i.e. associative key if assigned, otherwise the numerical key will be displayed - this goes for each item. As foreach literally means foreach, each item in the array will be printed (no arraycount versus numerical index issue).
Good luck with the exam !
Grz, Juliette
|
|
|
|
| Re: Difference between general array and PHP Arrays [message #1418 is a reply to message #1417 ] |
Sat, 28 July 2007 17:27   |
j.rf Messages: 139 Registered: October 2006 Location: The Netherlands |
Member |
|
|
Hi Marion,
| Marion wrote on Sat, 28 July 2007 22:56 | "Values in an associative array can be called both by their number as well as by their associative key."
Does this mean that associative arrays actually have 2 keys per value: a numeric key and a user specified (string) key?
|
To make things more confusing: yes and no.
Associative arrays don't have two keys per value, but in practice you can treat them as if they do as internally all arrays are assigned numerical keys no matter what.
(someone please correct me if I'm wrong)
So if you print_r an associative array, the keys you will see are the associative keys and those only - you will never see the numerical keys, but you *can* call a value / walk through an associative array using the numerical (internal) keys.
| Marion wrote on Sat, 28 July 2007 22:56 | "You can add numeric keys to associative arrays (or string keys to numeric arrays) To prevent confusion, shouldn't you call element by their proper key?
|
That is most definitely good coding practise ! and also a good reason to use foreach when walking through arrays rather than for loops (apart from foreach being faster most of the time and more reliable for making sure you call each element).
Even so, when preparing for an exam, I think you should know the kind of pittfalls / quirks mentioned above.
|
|
|
| Re: Difference between general array and PHP Arrays [message #1419 is a reply to message #1418 ] |
Sun, 29 July 2007 15:57  |
Marion Messages: 114 Registered: October 2006 Location: Amsterdam, The Netherland... |
Member |

|
|
It is interesting stuff... I was wondering about the "internal order" within arrays. I'd think this order to be the order in which elements are put into the array. Is that right?
If you would walk through an array using the pointer manipulation functions ( like next($arr), prev($arr), $current($arr) and $key($arr) ) would you get the elements in their internal order? The certification study guide states:
"..there is no correlation between the array pointer and the keys.."
which makes me think there must be a correlation with the internal order, since there must be some order to an array to begin with!
| Quote: | ..as internally all arrays are assigned numerical keys no matter what
|
These internal numerical keys, they would correlate with the internal order (?). If so, it poses the next question:
Do these internal numeric keys always correlate with the user defined numeric keys in numeric arrays? In other words, are associative arrays stored like numeric arrays, but with an 'extra' set of keys (string keys), or are all arrays stored exactly the same way?
Maybe I should try to think up some code to verify/falsefy this..
|
|
|