I have this code:
$data[1] = "blablabla";
$data[2] = "blablablabla";
if (strlen($data) < 10)
{
// doing...
}
In this code I want to check all elements from array. How to do it?
foreach ($data as $element) {
if (strlen($element) < 10) {
// Do something
}
}
If you want to modify the data, use a reference (add a & before $element):
foreach ($data as &$element) {
if (strlen($element) < 10) {
// Do something to $element
$element = "something else";
}
}
If you don't want to use references directly, you can use a standard for loop with an indexer:
for ($i = 0; $i < count($data); $i++) {
if (strlen($data[$i]) < 10) {
// Do something with $data[$i]
$data[$i] = "something else";
}
}
Use array_walk function of PHP. There are many examples on the linked PHP manual page.
Also take a look at array_map and array_filter functions.
for ($i = 0;$i < count($data);$i++){
if (strlen($data[$i]) < 10){
// process
}
}
Related
$numbers = array(3,5,6,7,8,11);
$missing = array();
for ($i = 3; $i < 11; $i++) {
if (!in_array($i, $numbers)){
$missing[] = $i;
}
}
I want to find the missing numbers from 3 to 11 without using PHP innuild function, i have tried but i haven't not completed fully.
In this code i have used in_array but without this i have to do. any one help here.I am new to PHP using PHP inbuild i can do this, but this is not my case.
Use foreach loop inside on $numbers and check for the value of $i. Declare a flag variable, say $found to false. While looping inside if we get the number, set $found to true and exit the loop. In the end, if $found still stays as false, add the current $i to the result.
<?php
$numbers = array(3,5,6,7,8,11);
$missing = array();
for ($i = 3; $i <= 11; $i++) {
$found = false;
foreach($numbers as $value){
if($value === $i){
$found = true;
break;
}
}
if(!$found) $missing[] = $i;
}
print_r($missing);
Online Demo
you can use array_diff same as :
$numbersOrigin = range(3, 11);
$numbers = array(3,5,6,7,8,11);
$missing = array_diff($numbersOrigin, $numbers);
unuse buildIn functions :
$numbers = array(3,5,6,7,8,11);
$missing = array();
$index = 0;
for ($i = 3; $i < 11; $i++) {
if ($numbers[$index] === $i) {
$index++;
} else {
$missing[] = $i;
}
}
Why this piece of code works when it is clearly wrong in the second for loop (for ($i==0; $i<$parts; $i++) {)?
Does php allows for multiple comparisons inside for loops?
function split_integer ($num,$parts) {
$value = 0;
$i = 0;
$result = [];
$modulus = $num%$parts;
if ($modulus == 0) {
for($i = 0; $i < $parts; $i++)
{
$value = $num/$parts;
$result[] = $value;
}
} else {
$valueMod = $parts - ($num % $parts);
$value = $num/$parts;
for ($i==0; $i<$parts; $i++) {
if ($i >= $valueMod) {
$result[] = floor($value+1);
} else {
$result[] = floor($value);
}
}
}
return $result;
}
Code for ($i==0; $i < $parts; $i++) runs because $i==0 has no impact on loop.
In normal for loop first statement just sets $i or any other counter's initial value. As you already set $i to 0 earlier, your loop runs from $i = 0 until second statement $i < $parts is not true.
Going further, you can even omit first statement:
$i = 0;
for (; $i < 3; $i++) {
echo $i;
}
And loop will still run 3 times from 0 to 2.
I have this piece of code that loops 1 through 99 and is a formula.
function getExperienceByLevel ($maxLevel)
{
$levels = array ();
$current = 0;
for ($i = 1; $i <= $maxLevel; $i++)
{
$levels[$i - 1] = floor ($current / 4);
$current += floor($i+300*pow(2, ($i/9.75)));
}
return $levels;
}
First you initiate it like so $aLevels = getExperienceByLevel(99); then to see how much EXP you need to get to level 6 you do this echo $aLevels[5]; since it's an array.
Now I'm trying to do reverse. Get Level by EXP.
function getLevelByExp($exp)
{
$myLevel = 0;
$aLevels = getExperienceByLevel(99);
for ($i = 1; $i < 100; $i++)
{
if ($exp > $aLevels[$i-1])
{
return $myLevel;
}
}
}
When called upon getLevelByExp(1124); or any number inside, it seems to return a zero. But it seems to work when you put echos inside that statement.
Like instead of return $myLevel do echo "You are up to level $i<br />"; and it will go all the way up to the current level you've gained EXP for.
But still.. doesn't work when I want to simply return a number.
This seems to work better than your function:
function getLevelByExp($exp)
{
$aLevels = getExperienceByLevel(99);
for ($i = 0; $i <= 99; ++$i)
{
//echo "cmp $exp >= aLevels[$i]={$aLevels[$i]}\n";
if ($exp <= $aLevels[$i])
return $i - 1;
}
return -1;
}
It needs improvement for the edge cases, such as when $exp is zero.
Return $i instead because it always '0'
if ($exp > $aLevels[$i-1]) {
return $i;
}
You never change $myLevel, so it will always stay at 0.
Try returning $i instead of $myLevel, as $i is actually changing:
function getLevelByExp($exp) {
$aLevels = getExperienceByLevel(99);
for ($i = 1; $i < 100; $i++) {
if ($exp > $aLevels[$i-1]) {
return $i;
}
}
}
I need some help, even though I think I'm checking for the length of the array and I should be breaking out of the loop, I still get warnings on my [else if ($value....] line. So either I'm missing something crucial or I've been staring at this code segment too long and its obvious. Any insight would be appreciated.
$count = count($filter); //Filter is an array
if ($count > 1 ){
//Compare values and generate a range to choose from
$i = 1;
foreach($filter as $value){
//Break the loop if at the end of the array
if ($i >= $count){
//throw new exception($i .' '.$count);
break;
}
//if the value is smaller then the next procceding value, because they are already in order of presidence,
//add it to our range of potentials.
else if($value < $filter[$i]->value){
array_push($range, key($filter));
}
$i++;
}
}else {
return false;
}
I suspect that there are gaps in your array. Try this:
$filter = array_values($filter); // this will remove any gaps in the array
$count = count($filter);
if ($count <= 1)
return false;
for ($i = 0; $i < $count; $i++)
{
if ($i != $count-1 && $filter[$i]->value < $filter[$i+1]->value)
array_push($range, key($filter));
}
Your array might have non-numeric keys. Then try this:
foreach($filter as $key=>$value)
{
// test for $filter[$key];
}
Or your $filter array doesn't hold objects, then you can't use the -> in
$filter[$key]->value
try this code.... no need of checking count..
$range = array();
$i = 1;
foreach($filter as $value)
{
if(isset($filter[$i]) && $value < $filter[$i]->value)
{
array_push($range, key($filter));
$i++;
}
else
{
break;
}
}
Here's the issue:
I retrieve the following data string from my database:
$row->exceptions = '1,2,3';
After explode I need the below code to check each one of the exploded pieces
$exceptions = explode(",", $row->exceptions);
//result is
//[0] => 1
//[1] => 2
//[2] => 3
for ($i = 0; $i <= $row->frequency; $i++) {
if ($exceptions[] == $i) {
continue;
} else {
//do something else
}
}
How can I make $exceptions[] loop through all keys from the exploded array so it evaluates if ==$i?
Thanks for helping.
It should suffice to substitute:
if($exceptions[] == $i)
with:
if(in_array($i,$exceptions))
By the way, it eliminates the need for a nested loop.
Ah, should be straightforward, no?
$exceptions = explode(",", $row->exceptions);
for ($i = 0; $i <= $row->frequency; $i++) {
foreach($exceptions as $j){
if($j == $i){
// do something
break;
}
}
}
I think I understand what you are asking. Here's how you would test within that loop whether the key equals $i.
for ($i = 0; $i <= $row->frequency; $i++)
{
foreach ($exceptions as $key => $value)
{
if ($key == $i)
{
continue;
}
}
}