View Full Version : array problem
vividona
03-16-2009, 10:31 AM
plz check this:
function info(){
$a = array(
'Name'=>'Othman',
'Nickname'=>'vividona',
'Age'=>'35',
'Nationalit'=>'Sudan');
foreach($a as $k=>$v){
while(list($k, $v) = each($a)){
echo "$k=>$v<br />";
}
}
}
info();
this code print all array except array[0] why???
JasonDFR
03-16-2009, 11:42 AM
To answer your question, when each() is called, it moves the array pointer forward. Thus when your loop runs for the first time, the pointer has already been moved to the second index and outputs 'Nickname' => 'Vividona' .
With that being said, I don't completely understand what is going on inside your function.
If someone could put this into english for me that would be great: list($k, $v) = each($a)
I know what list() and each() do separately, but I can't get my head around what list($k, $v) = each($a) is doing.
Also, like I answered above, it seems each is pushing the array pointer forward, and spitting out starting with the the second index. I guess this is happening because each() is called prior to the echo. Am I right?
techietim
03-16-2009, 11:42 AM
From what I see, this code looks a little redundant. This should be a better solution:
<?php
function info(){
$a = array(
'Name'=>'Othman',
'Nickname'=>'vividona',
'Age'=>'35',
'Nationalit'=>'Sudan');
foreach($a as $k => $v){
echo $k . '=>' . $v . '<br />';
}
}
As for why it was skipping the first item; it did that because when it got to the foreach statement, it advanced the array pointer one ahead.
EDIT:
Jason beat me to it.
JasonDFR
03-16-2009, 11:46 AM
EDIT:
Jason beat me to it.
Yeah, but that code confused Jason too. So I raised some questions of my own. It does seem like a crazy way to go about doing things.
Tim, can you explain what exactly is happening with this: list($k, $v) = each($a)
List goes through arrays, and each gets the "current key and value pair from an array and advance the array cursor."
http://us2.php.net/each
http://us2.php.net/list
techietim
03-16-2009, 12:07 PM
list (http://php.net/list) is a language construct which is used to assign array items to variables.
Example:
list($name, $age) = array('Tim', 105);
each (http://php.net/each) returns an array with the key and value of the current array element, then advances the array pointer.
Example:
$array = array('name' => 'Tim', 'age' => 105);
var_dump(each($array)); //0 => name, 1 => Tim
var_dump(each($array)); //0 => age, 1 => 105
var_dump(each($array)); //false
When it reaches the end, it returns false. So when used in a while loop, it loops through the array (starting at where the pointer is) and stops when it gets to the end.
JasonDFR
03-16-2009, 12:53 PM
Yeah, I more or less understand what each do separately, but when used like this:
list($k, $v) = each($a)
is kinda difficult for me to get my head around. each() returns an array with 4 elements. Both numeric keys and associative keys.
So list($k, $v) is assigning the value of the first two elements [0] and [1] of the array in each(), to $k and $v respectively?
I think this is what is happening. But I could be wrong. Since there are only two variables in list($k, $v), the remaining two values that are returned by each() are just ignored?
Also, I am thinking that there is no practical use for this. If there is one, I'd love to see an example. I really can't see what the point of Vividona's original function is. But again, I am totally capable of missing the point. So if there is one, please fill me in.
vividona
03-17-2009, 11:03 AM
thank you guys for your nices replies.
but how can I include the array[0] in foreach?
JasonDFR
03-17-2009, 11:11 AM
function info($a){
foreach($a as $k=>$v){
echo "$k=>$v<br />";
}
}
$a = array(
'Name'=>'Othman',
'Nickname'=>'vividona',
'Age'=>'35',
'Nationalit'=>'Sudan');
info($a);
Please tell me what you were trying to do with this: while(list($k, $v) = each($a))
CrazyChop
03-17-2009, 05:10 PM
Please tell me what you were trying to do with this: while(list($k, $v) = each($a))
I have no idea either.. It's not really needed.
the code will basically take an the key and value from array $a and assign it to $k and $v. But since the foreach has already move the iterator forward, when you reach the while(list...) part, the iterator is already pointing to the second element.
Either use the foreach, or the while(list($k, $v), but not both...
each() returns a array($key, $value) pair, not the whole array (that would be function id($a) { return $a; }). Basically,
foreach ($a as $k => $v) {
// ...
}
expands to:
while (list($k, $v) = each($a)) {
// ...
}
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.