$arr = Array ("A" =>1, "B" =>2, "C" =>3, "D" =>4, "E" =>5);
For example, I want to start the loop from C, how to write?
Reply to discussion (solution)
So you can get what you want? Gets the key name of the array and enters the loop if the key name equals C.
So you can get what you want? Gets the key name of the array and enters the loop if the key name equals C.
Can you give me a little more detail?
$arr = Array ("A" =>1, "B" =>2, "C" =>3, "D" =>4, "E" =>5);
$arr 1 = array_slice ($arr, 2); The next 2 of arr?
foreach ($arr 1 as $key = = $val) {
echo $key. ' = '. $val. '
';
}
$arr = Array ("A" =>1, "B" =>2, "C" =>3, "D" =>4, "E" =>5);
$arr 1 = array_slice ($arr, 2); The next 2 of arr?
foreach ($arr 1 as $key = = $val) {
echo $key. ' = '. $val. '
';
}
But this subscript is not necessarily in the second place, or maybe there are 10 in front of C, or 100?
$arr = Array ("A" =>1, "B" =>2, "C" =>3, "D" =>4, "E" =>5), $flag = 0;foreach ($arr as $key + $val) {if ($key = = ') C ') {$flag = 1;} if ($flag ==1) {echo $key. ' = '. $val. '
';}}
?? Good?.
$arr = Array ("A" =>1, "B" =>2, "C" =>3, "D" =>4, "E" =>5); $index = Array_search (' C ', Array_keys ($arr)); $arr 1 = Array_slice ($arr, $index), foreach ($arr 1 as $key = $val) {echo $key. ' = '. $val. '
';}
It's nice to say upstairs.
In fact #5 idea is right: iterate over, do not handle the data before the specified key appears
and #6 code needs to copy the specified key and later data, at least to consume more write memory
$arr = Array ("A" =>1, "B" =>2, "C" =>3, "D" =>4, "E" =>5); $flag = False;foreach ($arr as $key = + $val) { if (! ($flag = $flag? $flag: $key = = ' C ')) Continue; echo $key. ' = '. $val. '
';}
Written like this, more reasonable
$arr = Array ("A" =>1, "B" =>2, "C" =>3, "D" =>4, "E" =>5); foreach ($arr as $key = + $val) { if (! ($flag = $key = = ' C '? True:!empty ($flag))) Continue; echo $key. ' = '. $val. '
';}
Study, and understand the use of several array functions
$a = Array ("A" =>1, "B" =>2, "C" =>3, "D" =>4, "E" =>5); for (;; Next ($a)) { if (key ($a) = = ' C ') {while (list ($key, $val) = each ($a)) {echo "$key" + $val
";} Break;}}
Is there a way to get the position pointer in the array based on the key name
Start the loop directly from the pointer.
Traversing from the beginning is a waste of time, and the foreach create copy requests a piece of unnecessary memory
6 Floor and 9 floor code is very good, but used the assembly and C + + representation of the core code has obsessive-compulsive disorder ...
After dinner study the array functions in the PHP manual
$i = $v = 0;echo memory_get_usage (), php_eol; $a = Array (1,2,3,4,5,6,7), Echo memory_get_usage (), Php_eol;foreach ($a as $i =& gt; $v) { echo $i, ', Memory_get_usage (), Php_eol; break;} Echo Memory_get_usage (), Php_eol;
123792
124520
0 124520
124520
Obviously
foreach creates a copy requesting an unnecessary memoryis not correct
Is there a way to get the position pointer in the array based on the key name
Start the loop directly from the pointer.
Traversing from the beginning is a waste of time, and the foreach create copy requests a piece of unnecessary memory
6 Floor and 9 floor code is very good, but used the assembly and C + + representation of the core code has obsessive-compulsive disorder ...
After dinner study the array functions in the PHP manual
Is there a way to get the position pointer in the array based on the key name
Start the loop directly from the pointer.
Traversing from the beginning is a waste of time, and the foreach create copy requests a piece of unnecessary memory
6 Floor and 9 floor code is very good, but used the assembly and C + + representation of the core code has obsessive-compulsive disorder ...
After dinner study the array functions in the PHP manual
$i = $v = 0;echo memory_get_usage (), php_eol; $a = Array (1,2,3,4,5,6,7), Echo memory_get_usage (), Php_eol;foreach ($a as $i =& gt; $v) { echo $i, ', Memory_get_usage (), Php_eol; break;} Echo Memory_get_usage (), Php_eol;
123792
124520
0 124520
124520
Obviously
foreach creates a copy requesting an unnecessary memoryis not correct
Thank you Xu for correcting me ... Just learned that foreach is a direct loop of the original array, and then see which post or blog, foreach is a copy of the re-traversal, the content is really forgotten, $value create a copy should not refresh the memory ... I'll take a closer look
$i = $v = 0;echo memory_get_usage (), php_eol; $a = Array (1,2,3,4,5,6,7), Echo memory_get_usage (), Php_eol;foreach ($a as $i =& gt; $v) { echo $i, ', Memory_get_usage (), Php_eol; break;} Echo Memory_get_usage (), Php_eol;
123792
124520
0 124520
124520
Obviously
foreach creates a copy requesting an unnecessary memoryis not correct
It turns out my brain is wrong ...
'; foreach (FN () as $k = = $v) { echo memory_get_usage (). '
';} Echo Memory_get_usage (). '
';
Test the array of global variables and the array returned by the function separately, and the result is different, and foreach caches the result of the function, the loop ends immediately, and the existing variable is directly referenced
Of course it is.
YOU FN () The returned array must someone pick it up?
Without giving a real variable, PHP would have to put it on the stack.
Study, and understand the use of several array functions
You're here again, I'm level 3.