Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Searching Arrays

    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?

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You would create your array something like this:

    PHP Code:
    $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:

    PHP Code:
    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:
    PHP Code:
    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>';
        }

    Last edited by delinear; 06-23-2005 at 04:06 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •