0

I have an array built from a log file and I want to create a new array containing only the player names.

$list[2] is the location of the player names

             if(preg_match_all('/^(.*?) Player "(.*)" (.*) /m', $open, $list))
                {
                     foreach($list[2] as $name)
                     {
                        if(!in_array($name,$players))
                        {
                         $players[] = ["Player"=>$name];
                        }
                     }
                 }
                var_dump($players);

Its adding the player names to the array however it is adding even if an entry exists with the name as a value.

** Answer ** Was resolved following suggested answer by using

  $gt = array_unique($list[2]);
    foreach($gt as $name)
     {
      $players[] = ["Player"=>$name];
     }
ChrisYates
  • 31
  • 5
  • `$players[$name] = TRUE;` could work.. doesn't seem like `Player` would be needed. – user3783243 May 05 '22 at 13:45
  • Your current issue is documented here, https://stackoverflow.com/questions/4128323/in-array-and-multidimensional-array – user3783243 May 05 '22 at 13:49
  • This duplicate question does not represent a good "signpost" because there is no [mcve]. The intention is clear enough for programmers that understand your code, but having sample input and exact desired output would be better from a content quality point of view. Please never [edit] your question to include an "answer" -- that is not the job of a question. You can avoid calling `array_unique()` if you do an associative push `$players[$name] = ["Player" => $name];` because duplicate keys on the same level of an array are not allowed (they will be overwritten). – mickmackusa May 06 '22 at 03:17

1 Answers1

4

You can use array_unique function https://www.php.net/manual/en/function.array-unique.php

             if(preg_match_all('/^(.*?) Player "(.*)" (.*) /m', $open, $list))
                {
                     foreach($list[2] as $name)
                     {
                         $players[] = ["Player"=>$name];
                     }
                 }
                $players=array_unique($players);
                var_dump($players);

other solution:

             if(preg_match_all('/^(.*?) Player "(.*)" (.*) /m', $open, $list))
                {
                     foreach($list[2] as $name)
                     {
                        if(!in_array(["Player"=>$name],$players))
                        {
                         $players[] = ["Player"=>$name];
                        }
                     }
                 }
                $players=array_unique($players);
                var_dump($players);
designer132
  • 131
  • 7
  • Please flag/vote to close duplicate questions instead of answering them. When you answer duplicate questions, you make it harder for the Roomba to do its important work. I need to take this moment to spoil the romance -- in 2022, when someone asks a basic question, there are usually at least 5 duplicates to choose from. So, again, please do not answer basic duplicate questions. – mickmackusa May 06 '22 at 03:16