for($i = 0; $i < count($array); $i++) { /* … */ }
If the array is a numeric array with the keys from 0 to n-1, then it’s a useful way. It only works if all of the keys in the array are numeric.
for($i = 0; $i < count($array); $i++) {
print $array[$i]; // access
$array[$i] = “new value $i”; //modify
}
foreach($array as $val) { /* … */ }
It’s useful for both kinds of arrays. But in this way, the value could be accessed only.
foreach($array as $val) {
print $val; // access
}
foreach($array as $key => &$val) { /* … */ }
PHP5 introudces the possibility of modifying the contents of the array directly by assigning the value of each element to the iterated variable by reference rather than by value. Here, the $val is a reference of the element’s value. So changing $value inside foreach will also change the original value.
foreach($array as $key => &$val) {
print $val; //access
$val = “new value $key”; //modify
}
foreach($array as $key => $val) { /* … */ }
Here, $val is a copy of the value. foreach operates on a copy of $array. With the key of the element the value could be accessed and the original value could be changed with $array[$key].
foreach($array as $key => $val) {
print $val; //access
print $array[$key]; //access
$array[$key] = “new value $key”; //modify
}
while(list($key, $val) = each($array)) { /* … */}
Use $array[$key] to access and change the original element.
while(list($key, $val) = each($array)) {
print $val; //access
$array[$key] = “new value $key”; //modify
}
Details with example:
//using for($i = 0; $i < count($array); $i++) { /* … */ }
$array = array(‘foo’, ‘bar’);
for($i = 0; $i < count($array); $i++) {
print $array[$i]; // access
$array[$i] = “new value $i”; //modify
}
var_dump($array);
//using foreach($array as $val) { /* … */ }
$array = array(‘foo’, ‘bar’);
foreach($array as $val) {
print $val; // access
}
var_dump($array);
//using foreach($array as $key => &$val) { /* … */ }
$array = array(‘foo’, ‘bar’);
foreach($array as $key => &$val) {
print $val; //access
$val = “new value $key”; //modify
}
var_dump($array);
//using foreach($array as $key => $val) { /* … */ }
$array = array(‘foo’, ‘bar’);
foreach($array as $key => $val) {
print $val; //access
print $array[$key]; //access
$array[$key] = “new value $key”; //modify
}
var_dump($array);
//using while(list($key, $val) = each($array)) { /* … */}
$array = array(‘foo’, ‘bar’);
while(list($key, $val) = each($array)) {
print $val;
$array[$key] = “new value $key”; //modify
}
var_dump($array);
If the array is a numeric array with the keys from 0 to n-1, then it’s a useful way. It only works if all of the keys in the array are numeric.
for($i = 0; $i < count($array); $i++) {
print $array[$i]; // access
$array[$i] = “new value $i”; //modify
}
foreach($array as $val) { /* … */ }
It’s useful for both kinds of arrays. But in this way, the value could be accessed only.
foreach($array as $val) {
print $val; // access
}
foreach($array as $key => &$val) { /* … */ }
PHP5 introudces the possibility of modifying the contents of the array directly by assigning the value of each element to the iterated variable by reference rather than by value. Here, the $val is a reference of the element’s value. So changing $value inside foreach will also change the original value.
foreach($array as $key => &$val) {
print $val; //access
$val = “new value $key”; //modify
}
foreach($array as $key => $val) { /* … */ }
Here, $val is a copy of the value. foreach operates on a copy of $array. With the key of the element the value could be accessed and the original value could be changed with $array[$key].
foreach($array as $key => $val) {
print $val; //access
print $array[$key]; //access
$array[$key] = “new value $key”; //modify
}
while(list($key, $val) = each($array)) { /* … */}
Use $array[$key] to access and change the original element.
while(list($key, $val) = each($array)) {
print $val; //access
$array[$key] = “new value $key”; //modify
}
Details with example:
//using for($i = 0; $i < count($array); $i++) { /* … */ }
$array = array(‘foo’, ‘bar’);
for($i = 0; $i < count($array); $i++) {
print $array[$i]; // access
$array[$i] = “new value $i”; //modify
}
var_dump($array);
//using foreach($array as $val) { /* … */ }
$array = array(‘foo’, ‘bar’);
foreach($array as $val) {
print $val; // access
}
var_dump($array);
//using foreach($array as $key => &$val) { /* … */ }
$array = array(‘foo’, ‘bar’);
foreach($array as $key => &$val) {
print $val; //access
$val = “new value $key”; //modify
}
var_dump($array);
//using foreach($array as $key => $val) { /* … */ }
$array = array(‘foo’, ‘bar’);
foreach($array as $key => $val) {
print $val; //access
print $array[$key]; //access
$array[$key] = “new value $key”; //modify
}
var_dump($array);
//using while(list($key, $val) = each($array)) { /* … */}
$array = array(‘foo’, ‘bar’);
while(list($key, $val) = each($array)) {
print $val;
$array[$key] = “new value $key”; //modify
}
var_dump($array);
No comments:
Post a Comment