meltingpoint Posted July 15, 2010 Share Posted July 15, 2010 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 Quote Link to comment Share on other sites More sharing options...
meltingpoint Posted July 15, 2010 Author Share Posted July 15, 2010 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? Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted July 15, 2010 Share Posted July 15, 2010 Why not use a foreach to go through the array? Quote Link to comment Share on other sites More sharing options...
meltingpoint Posted July 15, 2010 Author Share Posted July 15, 2010 I'm stuck..............could you give an example. Additionally- it is imperative that the index of each, represented by $k, remain in tact and not be renumbered. Thanks Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted July 15, 2010 Share Posted July 15, 2010 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. } } Quote Link to comment Share on other sites More sharing options...
meltingpoint Posted July 15, 2010 Author Share Posted July 15, 2010 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 Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted July 15, 2010 Share Posted July 15, 2010 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? Quote Link to comment Share on other sites More sharing options...
meltingpoint Posted July 15, 2010 Author Share Posted July 15, 2010 I have used preg_grep on the whole array and preg_match on individual values and it still is the same. All get printed. Open to new ideas or whole new approach. Andy Quote Link to comment Share on other sites More sharing options...
meltingpoint Posted July 15, 2010 Author Share Posted July 15, 2010 Also just tired: for ($i = 1; $i <= count($hold)-1; $i++) { $k = count($hold)-$i; // if(in_array($search, $hold[$k]) == True); { echo "yes"; } } And everyone still evaluates to True! It seems that the if statement is not working inside the for loop. Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted July 15, 2010 Share Posted July 15, 2010 Out of curiosity do a check on a single value from the arrays that you know should be false right before the loop and see if it returns a false value. Quote Link to comment Share on other sites More sharing options...
meltingpoint Posted July 15, 2010 Author Share Posted July 15, 2010 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 Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted July 15, 2010 Share Posted July 15, 2010 Well that is odd... I don't get it. At least you got a workaround to work though. I don't know enough about preg to say why it does that. I'd be interested if someone does have an explanation for that as well. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.