PHP - Compare a string to all in array and only echo once (unique) -


i'm trying make list of directories file. want list contain unique entries i.e. - echo once no duplicates.

example: log file:

inactive : [2007-04-01 08:42:21] "home/club/member" 210.00 "r-200" inactive : [2008-08-01 05:02:20] "home/club/staff" 25.00 "r-200" active : [2010-08-11 10:12:20] "home/club/member" 210.00 "r-500" inactive : [2010-01-02 11:12:33] "home/premier/member" 250.00 "r-200" active : [2013-03-04 10:02:30] "home/premier/member" 250.00 "r-800" active : [2011-09-14 15:02:55] "home/premier/member" 250.00 "r-100" 

i want echo list of directories no duplicates:

home/club/staff

home/club/member

home/premier/member

i used foreach loop iterate through array don't know how compare each value each item in array , output identical items once.

foreach($listofdir $value)  {     echo "<p>" . $value .  "</p>"; } 

this should work you:

just file array , grab paths out of each line.

<?php      $lines = file("test.txt", file_ignore_new_lines | file_skip_empty_lines);      $directorys = array_unique(array_map(function($v){         preg_match("/.*? : \[.*?\] \"(.*?)\"/", $v, $m);         return $m[1];     }, $lines));      print_r($directorys);  ?> 

output:

array (     [0] => home/club/member     [1] => home/club/staff     [3] => home/premier/member ) 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -