-2

I've got a multidimensional array written in php that holds an array of arrays. I've read a lot about how to search this, but it seems most solutions either:

A. require you have unique values for the keys, such as a product id

or

B. are satisfied with returning multiple results in an array

I am looking to search the array given the round number (which is the array number of the highest/first level array), and a player name (which will be the value of either the key player 1 or player 2).

The array looks something like this:

Array ( 
            [0] => Array ( )
            [8] => Array ( 
                    [1] => Array ( 
                                  [Match] => 1 
                                  [Player1seed] => (Q) 
                                  [Player1name] => Mahut 
                                  [Player2seed] => (2) 
                                  [Player2name] => Goffin 
                                  [Matchscore] => 7-6(1), 6-1 
                                  [Round] => Finals 
                                  ) 
                           ) 

            [7] => Array   ( 
                   [1] => Array (
                                  [Match] => 1 
                                  [Player1seed] => (2) 
                                  [Player1name] => Goffin 
                                  [Player2seed] => 
                                  [Player2name] => Muller 
                                  [Matchscore] => 7-6(4), 6-4 
                                  [Round] => Semi-Finals 
                               ) 
                   [2] => Array
                    ( 
                                  [Match] => 2 
                                  [Player1seed] => (Q) 
                                  [Player1name] => Mahut 
                                  [Player2seed] => (WC) 
                                  [Player2name] => Haase 
                                  [Matchscore] => 5-7, 6-3, 6-4 
                                  [Round] => Semi-Finals 
                   ) 
             ) 

etc.

Essentially, I need to be able to search specifically one subset such as array[7] and be returned the results that contains either player1 or player2 as a name, say Goffin.

But I don't want it to return results from other tournament rounds such as array[8] or array[6] where either player is Goffin.

I can't seem to find this solution anywhere. Am I setting up my array incorrectly? Or expecting database functions from a lesser data set?

Any help would be appreciated.

Keith
  • 312
  • 2
  • 12
  • *I need to be able to search for array[7] that contains player2 as a name, say Goffin. But I don't want it to return array[8] where either player is Goffin.* - what a diff? – splash58 Jul 13 '15 at 09:32
  • In case there is some confusion about the array layout: The first dimension is the tournament Round number, 0-9 The second dimension is the Match number in that round 1-16 The third dimension is the details of the specific match. So I need to be able to locate a specific match in a specific round, and return a key that will allow me to pull the rest of the information for that specific match. An example, I know Goffin played in round 7, but I need to be able to locate the match details, how do I search to access the array stored in array [7][1] and get the match details. – Keith Jul 13 '15 at 19:34
  • @splash58 _It matters because the statistics for each match are different. But players in a tournament may play up to 7 matches in different rounds. I was trying to find the match a player participated in, given a specific round and the player name._ I will probably have to go back and just create a one dimensional array with 2 more parameters. I was just trying for a more elegant, and thus efficient solution by making each round its own array of matches. – Keith Jul 13 '15 at 20:48
  • i understood your logic in words, but you really need to change array structure, because indexes [7][1][player] and [8][1][player] dont show difference in meaning. – splash58 Jul 14 '15 at 05:47
  • I guess I'm really confused now. I think more like a database setup I guess where I think of [7][1][player] as a layout with table representing [round number] [match number] [player]. How would you expect the array be structured to accomplish this? @splash58 – Keith Jul 15 '15 at 08:22

1 Answers1

0

It's not exactly the way I wanted to solve the problem, but I was able to get the results I wanted by running a loop after identifying the specific round number:

$r = $roundnumber;

        foreach( $matchesarray[$r] AS $key=>$data ){

                $winnerseed=$data['Player1seed'];
                $winnername=$data['Player1name'];
                $loserseed=$data['Player2seed'];
                $losername=$data['Player2name'];
                $matchurl=$data['Matchurl'];
                $score=$data['Matchscore'];

                if ($p1name == $winnername || $p2name == $losername){
                    $winner=$p1;
                }
                else if ($p2name == $winnername || $p1name == $losername){
                    $winner=$p2;
                }

        }
Keith
  • 312
  • 2
  • 12