...

View Full Version : Searching Arrays



ptmuldoon
06-23-2005, 03:18 PM
Can some explain or provide an example of an array that would include items such as:

Player Number
Player Name
Week 1 Score
Week 2 Score
.
.
.
.
Week 10 Score

And then possible how I could search that array by player number or name, and be shown the correct information?

delinear
06-23-2005, 04:04 PM
You would create your array something like this:


$player_details[] = array('Player Number' => 'number', 'Player Name' => 'name', 'Week 1 Score' => 'wk1 score', ... etc up to ... 'Week 10 Score' => 'wk10 score');
Looping through your data from the database or whatever your source is.

You can then use in_array to match a search query and output the resultant array, for instance, let's assume you have the search keyword from a posted form field called "search", the following will iterate through your array and output all results that match:


for($i=0; $i < count($player_details); $i++) {
if(in_array($_POST['search'], $player_details[$i]);
echo '<pre>';
print_r($player_details);
echo '</pre>';
}
}

That will search all the values in each player's array - if you specifically want to match only the player name and number, try something like:

for($i=0; $i < count($player_details); $i++) {
if($player_details[$i]['Player Name'] == $_POST['search'] || $player_details[$i]['Player Number'] == $_POST['search']) {
echo '<pre>';
print_r($player_details);
echo '</pre>';
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum