I have a regex that loops through data like the following
23:10:54 User Name 1 has looted 598 x Dark Ochre
23:13:58 UserName 2 has looted 947 x Obsidian Ochre
22:55:29 User Name3 has looted 1509 x Onyx Ochre
22:55:29 User Name3 has looted 3 x Obsidian Ochre
The regex (below) correctly parses the data into the value $value['userName'];
$re = '/^(?<timeMined>[0-9]{2}:[0-9]{2}:[0-9]{2}) # timeMined
\s+
(?<userName>[\w\s]+) # user name
\s+(?:has\s+looted)\s+ # garbage text between name and amount
(?<amount>\d+) # amount
\s+x\s+ # multiplication symbol
(?<item>.*?)\s*$ # item name (to end of line)
/xmu';
preg_match_all($re, $sample, $matches, PREG_SET_ORDER);
In the data set below, there are 3 distinct user names. I am attempting to loop through the data and add a user name to an array if it doesn’t already exist. This is what I currently have. However, I only get 1 result, User Name3. I’m confused as to where the issue lies here.
foreach ($matches as $value){
$userName = $value['userName'];
echo $userName."<BR>";
$userNameArray = array();
if (in_array($userName, $userNameArray)){
}
else {
array_push($userNameArray, $userName);
}
}
echo "<BR><BR><BR><BR><BR>";
foreach ($userNameArray as $value){
echo $value."<BR>";
}
This code results in the following. The top part is accurate, the bottom however, is missing two user names
User Name 1
UserName 2
User Name3
User Name3
User Name3
you delete your array in the for loop in each iteration 😉