How to query PHP arrays from a specified position? $ Arr = array ("a" => 1, "B" => 2, "c" => 3, "d" => 4, "e" => 5 );
For example, how do I write a loop starting from c?
Reply to discussion (solution)
In this way, what do you want? Obtain the key name of array. if the key name is equal to c, it enters the loop.
In this way, what do you want? Obtain the key name of array. if the key name is equal to c, it enters the loop.
Can it be detailed?
$ Arr = array ("a" => 1, "B" => 2, "c" => 3, "d" => 4, "e" => 5 );
$ Arr1 = array_slice ($ arr, 2 );//? Under arr? 2? Start to retrieve
Foreach ($ arr1 as $ key => $ val ){
Echo $ key. '='. $ val .'
';
}
$ Arr = array ("a" => 1, "B" => 2, "c" => 3, "d" => 4, "e" => 5 );
$ Arr1 = array_slice ($ arr, 2 );//? Under arr? 2? Start to retrieve
Foreach ($ arr1 as $ key => $ val ){
Echo $ key. '='. $ val .'
';
}
However, this subscript may not be in the second place. maybe there are 10 or one hundred in front of c?
$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.'
';}}
?? Okay ?.
$arr = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);$index = array_search('c',array_keys($arr));$arr1 = array_slice($arr, $index);foreach($arr1 as $key=>$val){echo $key.'='.$val.'
';}
The upstairs is good ..
In fact, the idea of #5 is correct: traverse it and not process the data before the specified key appears.
#6 of the code needs to copy the specified key and subsequent data, at least need 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.'
';}
This is 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.'
';}
I learned how to use 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 obtain the position pointer in the array based on the key name?
Starts directly from the pointer during the loop.
It is a waste of time to traverse from the ground up, and the foreach creates a copy to request an unnecessary memory.
The code on the 6th and 9th floors is good, but the compilation and C ++ are used to indicate obsessive-compulsive disorder on the core code...
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=>$v) { echo $i, ' ', memory_get_usage(), PHP_EOL; break;}echo memory_get_usage(), PHP_EOL;
123792
124520
0 124520
124520
Apparently
Foreach creates a copy and applies for an unnecessary memory.Is incorrect
Is there a way to obtain the position pointer in the array based on the key name?
Starts directly from the pointer during the loop.
It is a waste of time to traverse from the ground up, and the foreach creates a copy to request an unnecessary memory.
The code on the 6th and 9th floors is good, but the compilation and C ++ are used to indicate obsessive-compulsive disorder on the core code...
After dinner, study the array functions in the php Manual
Is there a way to obtain the position pointer in the array based on the key name?
Starts directly from the pointer during the loop.
It is a waste of time to traverse from the ground up, and the foreach creates a copy to request an unnecessary memory.
The code on the 6th and 9th floors is good, but the compilation and C ++ are used to indicate obsessive-compulsive disorder on the core code...
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=>$v) { echo $i, ' ', memory_get_usage(), PHP_EOL; break;}echo memory_get_usage(), PHP_EOL;
123792
124520
0 124520
124520
Apparently
Foreach creates a copy and applies for an unnecessary memory.Is incorrect
Thanks to xu dianzheng... when I first learned this, I thought that foreach directly loops through the original array. then I saw the post or blog. foreach copied a copy and then traversed it. I really forgot the content, $ value creating a copy should not refresh the memory... 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=>$v) { echo $i, ' ', memory_get_usage(), PHP_EOL; break;}echo memory_get_usage(), PHP_EOL;
123792
124520
0 124520
124520
Apparently
Foreach creates a copy and applies for an unnecessary memory.Is incorrect
It turns out that my brain is wrong...
';foreach (fn() as $k => $v) { echo memory_get_usage() . '
';}echo memory_get_usage() . '
';
Test the global variable array and the array returned by the function respectively. The result is different. foreach caches the function running result and immediately releases the result after the loop ends. When an existing variable exists, it is directly referenced.
Of course.
Will someone pick up the array returned by fn?
Php should put it in the stack if no real variable is provided.
I learned how to use several array functions.
You are here again. I have 3 levels.