/    Sign up×
Community /Pin to ProfileBookmark

Help in for loop….

How come I cant do this? Please help….

[code=php]
for ($m=0; $m < $option; $m++)
{
for($i=$m; $i<$d; $i=($m + $option))
{
if ($value[$i] == $value1[$k])
{
$match = $match + 1;
$k++;
}
else
{
$k++;
}
}
echo $match;
}
[/code]

The problem lies with the for loop saying $i=$m. How come I cant do this?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 26.2006 — What is the code supposed to do? What is it not doing, or what error is being generated, which causes you to say it's not working and that the problem is with the $i=$m ?
Copy linkTweet thisAlerts:
@dannyjoyauthorFeb 26.2006 — i've tried it without the $i = $m and it works....the $i = $m is to make it loop for another specified number of times.

when i put $i = $m, the loop doesnt end
Copy linkTweet thisAlerts:
@dannyjoyauthorFeb 26.2006 — actually it was only

for($i=0; $i<$d; $i=($i + $option))

{

if ($value[$i] == $value1[$k])

{

$match = $match + 1;

$k++;

}

else

{

$k++;

}

}

echo $match;

and this works....but because I want to make this whole loop to loop for another specified number of times , that's why I put in the other for loop....
Copy linkTweet thisAlerts:
@NogDogFeb 26.2006 — You've got 7 variables and 2 arrays in the mix here. Without knowing the values of each, how can we analyze what's going on? This following works fine, but I had to initialize many of the variables and the arrays:
[code=php]
<?php
$option = 5;
$value = array (1,2,3,4,5,6,7,8);
$value1 = array (1,2,3,4,5,6,7,8);
$k = 0;
$match = "0";
$d = 5;
for ($m=0; $m < $option; $m++)
{
for($i=$m; $i<$d; $i=($m + $option))
{
if ($value[$i] == $value1[$k])
{
$match = $match + 1;
$k++;
}
else
{
$k++;
}
}
echo $match;
}
?>
[/code]
Copy linkTweet thisAlerts:
@Mester_PedizFeb 26.2006 — You really need to post the whole script!
Copy linkTweet thisAlerts:
@dannyjoyauthorFeb 26.2006 — [code=php]
$criteria = 5;
$option = 4;
$d = ($criteria * $option);
$match = 0;
$k=0;
$value = array(1,2,3,4,2,3,4,1,2,3,4,3,2,3,4,1,3,3,2,1)
$value1 = array(1,2,3,2,3)

for ($m=0; $m < $option; $m++)
{
for($i = $m; $i < $d; $i = ($m + $option))
{
if ($value[$i] == $value1[$k])
{
$match = $match + 1;
$k++;
}
else
{
$k++;
}
}
echo $match;
}
[/code]


I'm supposed to compare only certain values of array value with the value of array value1 and if they are equal, increase the match...which is just a counter to keep count...

It still doesnt work....
Copy linkTweet thisAlerts:
@NogDogFeb 26.2006 — This is an infinite loop:
[code=php]
for($i = $m; $i < $d; $i = ($m + $option))
[/code]

I think maybe what you want is:
[code=php]
for($i = $m; $i < $d; $i += ($m + $option)) // += instead of =
[/code]
×

Success!

Help @dannyjoy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...