Is there a way of iterating through an array but performing an operation on every other element?
ie If I have an array with 13 elements how do I do something to only elements 2,4,6,8,10 and 12?
foreach($array as $val) {
if(($i++ % 2) == 0) {
...do stuff here...
}
}
for ($i=1; $i<sizeof($array); $i+=2) {
// do stuff to $array[$i]
}
You can integrate it into a foreach loop too:
$i = 0;
foreach ($array as $v) {
if ($i++ & 1) continue;
// do stuff to $v
}
Note: $i & 1 is equivalent to ($i % 2) == 1 (or just $i % 2).
to fix cletuses answer for more speed and fix typos:
for ($i = 1, $j = count($array); $i < $j; $i += 2) {
// code
}
Another variation on the answers already posted... Similar to Phil Carter's answer. If the array has a numeric index you can use that in the foreach instead of managing a separate counter variable:
foreach ($array as $i => $v) {
if (! ($i % 2)) {
// do stuff to $v
}
Related
I have a multidimentional array of 5 items and I want that my loop would compare it like:
1 -> 2, 1 -> 3, 1 -> 4, 1 -> 5, 2->1, 2->3, 2->4, 2->5......// so on and 5 -> 4 in the end.
The problem is that after my array $i value matches 1 and $j value matches 3, the unset is done and the $i value becomes 2 (which is correct) and $j value becomes 4 instead of 3. Could someone tell me why and what I'm doing wrong?
My loop is:
for ($i = 0; $i <= count($myArray); $i++) {
for ($j = $i+1; $j <= count($myArray); $j++) {
if (
// condition 1
&& // condition 2
) {
unset($myArray[$i]);
$i++;
}
}
}
I think that's the problem is when you unset the element in the array, you increment the counter of the loop $i. In this way, the elements of the array that are not configured are removed, this empty array position will be maintained, it will not be reordered, you will have to do it manually or using array_values method.
In the last tour of the array, it will break because you are comparing the number of array elements as equal. You must use index < count($array)
The code would be like this:
for ($i = 0; $i < count($myArray); $i++) {
for ($j = $i+1; $j < count($myArray); $j++) {
if (
// condition 1
&& // condition 2
) {
unset($myArray[$i]);
// $i++;
}
}
}
try something like this
for ($i = 0; $i <= count($myArray); $i++) {
for ($j = 0; $j <= count($myArray); $j++) {
if ($j!=$i)
{
if (
// condition 1
&& // condition 2
) {
unset($myArray[$i]);
$i++;
}
}
}
}
$temp = $myArray;
for ($i = 0; $i <= count($myArray); $i++)
{
for ($j = $i + 1; $j <= count($myArray); $j++)
{
if (
// condition 1
&& // condition 2
)
{
unset($temp[$i]);
$i++;
}
}
}
print_r($temp);
Your result is in $temp. So here indexes wont get hampered, you actually are applying all operation on $temp and normally traversing $myArray.
To be honest, I do not know why nobody has yet advise to use nested foreach, since you all noticed there was a problem with array size.
foreach ($numbers as $number_horizontal_parsing) {
foreach ($numbers as $number_vertical_parsing) {
if ($number_horizontal_parsing != $number_vertical_parsing) {
//do your stuff in your case it seems you want to compare both variables
}
}
}
Here is my code, and it is not removing $arr[5] element so that I am trying to remove strings starting with # from my array
this is code
<?php
$arr = [
'#EXTM3U',
'#EXTINF:177,Paul Dateh & Oren Yoel - Be More',
'Be More.mp3',
'#EXTINF:291,Christopher Toy - Just Because',
'Just Because.mp3',
'#EXTINF:238,Magnetic North - Drift Away',
'Drift Away.mp3'
];
for ($i = 0; $i <= count($arr); $i++) {
if ($arr[$i]{0} == '#') {
echo $arr[$i] . "\n";
unset($arr[$i]);
}
}
print_r($arr);
?>
Reason:- You are counting array length inside the loop and every time when any value got unset() from the array, length of array decreased and value of count($array) changed (simply decreased)
So logically your 5th and 6th element never goes through if condition (they never get traversed by loop because of decreasing length of the array )
Solution 1:- Put count outside and it will work properly:-
$count = count($arr);
//loop start from 0 so use < only otherwise, sometime you will get an undefined index error
for ($i = 0; $i < $count; $i++) {
if ($arr[$i]{0} == '#') {
//echo $arr[$i] . "\n";
unset($arr[$i]);
}
}
print_r($arr);
Output:-https://eval.in/996494
Solution 2:- That's why i prefer foreach() over for() loop
foreach($arr as $key=> $ar){
if ($ar[0] == '#') {
unset($arr[$key]);
}
}
print_r($arr);
Output:-https://eval.in/996502
more spacific :
for ($i = 0; $i < count($arr); $i++) {
if (strpos($arr[$i], '#') !== false) {
echo "<br/>";
} else {
echo $arr[$i]."<br/>";
}
}
Try to use additional array to push right values. You calc count($arr); each iteration and when you do count($arr); your array gets smaller and count($arr); returns smaller values, so last elements won't be comparing, try to use variable to calc count before loop make changes:
<?php
//...
$start_count = count($arr);
for ($i = 0; $i <= $start_count; $i++) {
if ($arr[$i]{0} == '#') {
echo $arr[$i] . "\n";
unset($arr[$i]);
}
}
Or remove bad element with a help of additional array, put good elements in new array and don't delete them from input array:
<?php
$arr = [
'#EXTM3U',
'#EXTINF:177,Paul Dateh & Oren Yoel - Be More',
'Be More.mp3',
'#EXTINF:291,Christopher Toy - Just Because',
'Just Because.mp3',
'#EXTINF:238,Magnetic North - Drift Away',
'Drift Away.mp3'
];
$cleared_from_mess_array = array();
for ($i = 0; $i <= count($arr); $i++) {
if ($arr[$i]{0} != '#')
{
array_push($cleared_from_mess_array,$arr[$i]);
}
}
print_r($cleared_from_mess_array);
exit;
How would I output only a set of numbers in an array, say if theres 10 arrays, I would only like to output 8 of them?
foreach($arrays as $array){
//do I use a for loop/
}
Thanks!
foreach is only the natural approach if you actually want to iterate over each item (as the name implies). However, you could do something like this:
$i = 0;
foreach($arrays as $array){
...
$i++;
if ($i == $limit) {
break;
}
}
$i = 0;
foreach($arrays as $array){
if($i < 8){
// do something
}
$i++;
}
foreach(array_slice($arrays, 0, 8) as $array){
//do I use a for loop/
}
You could use a foreach loop like this (already mentioned).
$i = 0;
foreach($arrays as $array){
//do I use a for loop/
if(++$i >= 8) break;
}
...or you can use a for loop, which is designed for doing actions a set number of times, e.g., 8 times.
for($i=0; $i < 8; $i++){
$array = $arrays[$i];
// body
}
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;
}
}
}
How can I optimize the following code ,
I Need to run 3 sets of loops like this:
for($i=1;$i<=$count-1;$i++){
for($j=$i+1;$j<=$count;$j++){
// do some query use $i and $j
}
}
for($i=1;$i<=$count-2;$i++){
for($j=$i+1;$j<=$count-1;$j++){
for($k=$j+1;$k<=$count;$k++){
// do some query use $i and $j and $k
}
}
}
for($i=1;$i<=$count-3;$i++){
for($j=$i+1;$j<=$count-2;$j++){
for($k=$j+1;$k<=$count-1;$k++){
for($l=$k+1;$l<=$count;$l++){
// do some query use $i and $j and $k and $l
}
}
}
}
Is there a way to simplify the code, perhaps to connect the loops together ?
thanks !
This should do it (untested):
for($i = 1; $i <= $count - 3; $i++) {
for($j = $i + 1; $j <= $count; $j++) {
// i,j query
if($j > $count - 2) {
continue;
}
for($k = $j + 1; $k <= $count; $k++) {
// i,j,k query
if($k > $count - 1) {
continue;
}
for($l = $k + 1; $l <= $count; $l++) {
// i,j,k,l query
}
}
}
}
Note that the queries are no longer in their original order.
As it has been said, there's no way to optimize this further without knowing the queries you are running.
The big problem is that the inner loops are run multiple times. You can get around this by checking i === 1 and j === 2 inside the loops and only running the appropriate code if true.
Micro-optimization:
Use
++$i
rather than
$i++
and equivalent for $j++, $k++ and $l++
But what are you doing in these loops: it's entirely possible that your do some query (database?) could be changed to remove the loops completely... and that would be far more effective than any micro-optimisations