Jump to content

for loop- test to see of variable is in array with in loop


meltingpoint

Recommended Posts

Ok.  Have been working on this for some time and cannot get it.

$search_variable
//
//-------loop through $hold (reverse order) creating an multi-dim array indexed by $k
for ($i = 1; $i <= count($hold)-1; $i++)
{
$k = count($hold)-$i;
//
//------------need to test if $search_variable is in each of the newly
//------------indexed arrays keeping the indexing in order
//------------if the $search_variable is in the array- echo the variables in that array 
//
$cat = $hold[$k][1];
$name = $hold[$k][2];
//
echo "<tr>";
echo "<td class='td tc w200 ol14'><a href=\"dcdataedit.php?dccat=$cat&dcrequest=$k\" target='_top'>View / Edit / Delete</a></td>";
echo "<td class='ff tc w200 fwb ol14'>$cat</td>";
echo "<td class='td tc w200 ol14'>$name</td>";
echo "</tr>";
}

 

I have tried numerous methods and in each- It does not seem to want to allow for an if statement inside the loop.  Additionally- it keeps printing all the arrays- not just the ones that have the $search_variable match.

 

Any suggestions welcome.

Andy

Link to comment
Share on other sites

Sorry guys....I have tried the following with negative results

for ($i = 1; $i <= count($hold)-1; $i++)
{
$k = count($hold)-$i;
if(preg_grep("`\b$search`i", $hold[$k]) == False);
{
echo " Sorry No Match";
}
if(preg_grep("`\b$search`i", $hold[$k]) == True);
{
//
$cat = $hold[$k][1];
$name = $hold[$k][2];
//
echo "<tr>";
echo "<td class='td tc w200 ol14'><a href=\"dcdataedit.php?dccat=$cat&dcrequest=$k\" target='_top'>View / Edit / Delete</a></td>";
echo "<td class='ff tc w200 fwb ol14'>$cat</td>";
echo "<td class='td tc w200 ol14'>$name</td>";
echo "</tr>";
}
}

 

 

What am I doing wrong?

Link to comment
Share on other sites

http://us3.php.net/manual/en/control-structures.foreach.php

 

If you want to check the search variable to a value for a single dimension array do it like this...

Same principles apply to multi once you've a grasp on the foreach.

 

But since not exactly sure what you are doing... ie not sure what's in the array...number of arrays within the first array and what is contained in those arrays... etc... I can't give specific example not sure exactly what you are trying to accomplish I mean without knowing some example content.

 

Also check out the php.net site and look at the examples and comments for many situations and examples that may help.

 

//we have an array $hold
//we have a search variable $search

foreach ($hold as $key => $value) {
     // the array Key: $key 
     //  the array Value: $value
    if($value == $search)
     {
         //Do something here since the value is equal to search.
      }
}

Link to comment
Share on other sites

gwolgamott-- I really appreciate the time and help.

 

each $hold[$k] would contain an array with numeric index and values that range from single word to multiple words.  That is why I am trying to test each $hold[$k] using preg_grep() as it tests the entire array for the pattern found in $search.  If it does match- I want to echo out the $hold[$k] array that the match was in.

 

I have tried a number of times with if statements, foreach and for and each time it prints all the $hold[$k] arrays.  It is as if it does not evaluate the condition.

 

I hope that make it a little clearer and hope that it aids in the solution to this problem.

 

Much thanks- Andy

Link to comment
Share on other sites

I have tried a number of times with if statements, foreach and for and each time it prints all the $hold[$k] arrays.

Ok I see, sounds like for some reason or another that the preg-match is returning true every time.

Have you attempted to use something else just to test that yet?

Link to comment
Share on other sites

On a single value- it will return false if it does not match.  Adding the loop causes it to evaluate to true.  Odd.

 

I was able to accomplish my task in another manner- see below.

$openedfile = file($file2);
$match  = preg_grep("`\b$search`i", $openedfile);
if(!$openedfile)
{
echo "<center><strong>File could not be opened- please notify the Administrator.</strong></canter>";
exit;
}
if(empty($match))
{
echo "<tr>";
echo "<td class='td tc w200 ol14'>There were no matches to your query.</td>";
echo "</tr>";
}
else
{
foreach($match as $key => $individual_match)
	{
	$matched[$key] = explode("|", $individual_match);
	$cat = $matched[$key][1];
	$name = $matched[$key][2];
	echo "<tr>";
	echo "<td class='td tc w200 ol14'><a href=\"dcdataedit.php?dccat=$cat&dcrequest=$key\" target='_top'>View / Edit / Delete</a></td>";
	echo "<td class='ff tc w200 fwb ol14'>$cat</td>";
	echo "<td class='td tc w200 ol14'>$name</td>";
	echo "</tr>";
	}
}
echo "</table>";

 

While I would have preferred to compile a large array and then loop through it rather than read the entire database into file()- that is the only way to make it work keeping the keys associated to each array that matched.  This was crucial to do due to choosing a file to view, edit or delete.  I believe it will be ok as the entire size of the database will likely never exceed 5mb in size and it should not slow things significantly.

 

Still interested in a solution to the loop if anyone figures it out.

 

Thanks- Andy

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.