Please see my code, i am almost stuck in this, Why the break inside the array_walk not breaking...
$bool=array_walk($_POST, 'check_empty');
function check_empty($item, $key)
{
$bool=(isset($item) && $item != "") ? 1: 0 ;
if(!$bool)
{
//return 0;
break;
}
return $bool;
}
It's not at all clear what you're trying to do, but break is a control structure that exits only true loops (for, foreach, while and do-while) and switch structures.
array_walk is iterative but not a loop in the true sense.
You can't 'break' (to use the terminology) from an array walk callback; it is invoked on each element of the array as a means to update or otherwise modify each element, and I can't imagine a use case where you'd want to terminate this during it.
What are you trying to do? Your break is not working, because you're returning a value right before it - so the line with break is never executed.
If you're trying to remove all empty values from the array, try array_filter($array).
Related
How can I rewrite this piece of php script to not use the deprecated function 'each' anymore in the 'if' statement ?
if (list($_basic_auth_realm, $_basic_auth_header) = each($_auth_creds))
{
...
}
Many thx in adv. for you input !
V.
[EDIT]. This if statement is not in a loop. It's part of an larger block:
if (!empty($_basic_auth_header))
{
....
}
else if (!empty($_basic_auth_realm) && isset($_auth_creds[$_basic_auth_realm]))
{
....
}
else if (list($_basic_auth_realm, $_basic_auth_header) = each($_auth_creds))
{
....
}
$_basic_auth_realm, $_basic_auth_header are strings
$_auth_creds is an array
I don't really understand how this 'if' statement works. I only attempt to update the script which returns warnings when executed. It is used as a php proxy on my NAS as was written by Abdullah Arif: https://github.com/emersion/phproxy
Use your own variable to keep track of the current index in the array, rather than depending on the internal state of the array.
$auth_creds_index = 0;
...
else if (list($_basic_auth_realm, $_basic_auth_header) = $_auth_creds[$auth_creds_index++])
Each place where you currently use each($_auth_creds) should use $_auth_creds[$auth_creds_index++], they'll get successive elements of the array.
If you reassign the variable with a new array, you need to reset the variable back to 0.
You could also define a class wrapper for the array that automates all this.
I tried to google this and i think it can't be done but i have to ask anyway.
If i have while loop?
while ($smth > 1)
{
func();
}
Is there a way i could exit the while loop immediately with that function inside it?
Maybe that function returns something that stop loop?
I tried
func(){
return break;}
but this doesn't work.
It's not possible the way you've done it -- you can't return a break. But you can return a value that could signal to exit the loop.
function func() {
return true
}
And then
while($smth > 1) {
if(func()) break;
}
This will run the function, but it will also check it's return value to verify it does not need to exit the loop. If the function returned true, the if statement is satisfied and so a break will occur. If the function doesn't return explicitly, the if will not be satisfied.
You sort of can get out of that while loop from inside that function by throwing an exception. Your teammates will not like you anymore, however.
I have a part in my code:
while($a = getrow()){
//code
}
getrow() is a function which keeps on returning array based on some condition.
what getrow() should return so that the while loop doesn't execute the code inside but takes the next value returned by getrow() function.
While loops will run as long as the condition remains true. So as long as you return rows, the code inside will get executed. If you return false, the while loop will terminate. If you want to conditionally avoid running code within the loop, your option is to return something like 'SKIP' and then inside the while loop check if $a == 'SKIP' and then issue a continue.
while($a = getrow()){
if($a == 'SKIP')
continue;
//code
}
You can use continue control structure for skip an iteration. Please read the docs
while($a = getrow()){
if($a == 'something'){
continue; // skip iteration
}
//rest code which you want to run
}
if i'm looping over an array, and while in the middle of one of the loops i discover some small issue, change ...something..., and need to try again ... is there a way to jump back to the top of the loop without grabbing the next value out of the array?
i doubt this exists, but it would be some keyword like continue or break. in fact, it would be a lot like continue, except that it doesn't get the next item, it maintains what it has in memory.
if nothing exists, can i insert something into the array in such a way that it will become the next key/value in the loop?
maybe this would be easier with a while(array_shift())...
or i suppose a recursive function inside the loop might work.
well, my question is evolving as i type this, so please review this pseudo code:
foreach($storage_locations as $storage_location) {
switch($storage_location){
case 'cookie':
if(headers_sent()) {
// cannot store in cookie, failover to session
// what can i do here to run the code in the next case?
// append 'session' to $storage_locations?
// that would make it run, but other items in the array would run first... how can i get it next?
} else {
set_cookie();
return;
}
break;
case 'session':
set_session();
return;
break;
}
}
i'm sure there is no keyword to change the value tested against in the switch mid-stream... so how should i refactor this code to get my failover?
Not with a foreach, but with more manual array iteration:
while (list($key, $value) = each($array)) {
if (...) {
reset($array); // start again
}
}
http://php.net/each
http://php.net/reset
It seems like a simple fall through would do the trick though:
switch ($storage_location) {
case 'cookie':
if (!headers_sent()) {
set_cookie();
break;
}
// falls through to next case
case 'session':
Why this is not possible to accomplish?
foreach($arr as $k => $v)
{
if($condition) { $obj->myMethod() && continue; }
}
After $obj->myMethod() gets evaluated then the keyword continue is evaluated (executed), resulting in skipping the current iteration.
EDIT: i'm asking this because something like:
if($error) { $log->fatal('Something weird happened.') && continue; }
is single line and self-explanatory.
continue is a statement not an expression.
And never the twain shall meet.
You can't put a statement in an expression. (What would echo false && continue; print?)
Instead, use an if, which can contain statements.
You cannot evaluate continue as a condition. The continue keyword does not work the same way as in other languages. In PHP, depending on context continue and break can be somewhat synonymous, consider this construct:
<?php
switch ($months)
{
// start with vowels
case 'august':
break;
case 'april':
continue; // exactly the same as "break" !!!
default:
return 'OK';
}
throw new StartsWithVowelException('Months with vowels are creepy');
?>
While we are on the topic, the break and continue keywords have a feature in PHP that make them a bit more interesting and powerful than their peers in other languages.
Both can be given a numerical argument when used in a loop that indicates how many loops to continue through or break out of. For example, here is an example that restarts the execution of an outer loop from within an inner one::
<?php
//
// verify that each sub array contains the given value
//
$lowerval = strtolower($value);
foreach ($TwoDArray as $otherArray)
{
foreach ($otherArray as $value)
{
if (strtolower($value) == $lowerval)
{
// we found the value -- this one definitely has it.
continue 2;
}
}
// if we've reached here, then the inner loop doesn't have the
// value. ¡aiiee!
}
?>
Hope this helps you out with these 2 constructs, good-luck.
continue is a statement. In PHP a statement and an expression are two different things, statements cannot be evaluated because they do not by nature return true or false which is a requirement for evaluation in PHP.
In PHP you'd have to do something like:
if(test()) continue;