webdeveloper123 Posted March 8 Share Posted March 8 Hi Guys, I am writing a script that pulls 200+ domain names from a db table, with the intent on displaying only the .tld extension of each. I have done that part, works fine. The 2nd part: Now with that data, I want to create an array then use array_unique and display only the unique .tld, write some statistics about them (e.g 20% of your domain names are .com) and possibly make a pie chart at the end. I use this: $unique = explode(" ", $tld); To convert the string to the array, but when I print_r the array I get only one element in the array (the very last one in the db table) Array ( [0] => .com.au ) Here is my full code: <?php include 'includes/db.php'; $sql = "SELECT domain_names FROM domains;"; $statement = $pdo->query($sql); $urls = $statement->fetchAll(); $unique = []; foreach($urls as $url){ $tld = strstr($url['domain_names'], '.'); echo "$tld <br> "; } $unique = explode(" ", $tld); echo '<pre>',print_r($unique,1),'</pre>'; echo '<pre>',print_r($urls,1),'</pre>'; ?> Here is the output (sample) of the echo $tld statement: .co.uk .co.uk .co.uk .uk .co.uk .co.uk .uk .co.uk .uk .co.uk .uk .uk .co.uk .uk All the .tld match up with the db data. Here is a sample of the array $urls Array ( [0] => Array ( [domain_names] => camera.co.uk ) [1] => Array ( [domain_names] => garage.co.uk ) [2] => Array ( [domain_names] => buyer.co.uk ) [3] => Array ( [domain_names] => lane.uk ) [4] => Array ( [domain_names] => cctv.co.uk ) [5] => Array ( [domain_names] => track.co.uk ) [6] => Array ( [domain_names] => track.uk ) [7] => Array ( [domain_names] => sonytv.co.uk ) [8] => Array ( [domain_names] => sonytv.uk ) [9] => Array ( [domain_names] => programs.co.uk ) [10] => Array ( [domain_names] => media.uk ) [11] => Array ( [domain_names] => guide.uk ) [12] => Array ( [domain_names] => batteries.co.uk ) [13] => Array ( [domain_names] => batteries.uk ) ) So my question is, how do I get every single .tld extension that's in $tld into the array? Thanks Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted March 8 Solution Share Posted March 8 You may echo all of them but, each time through the loop, $tld is overwritten - you are just left with last one. Quote Link to comment Share on other sites More sharing options...
webdeveloper123 Posted March 8 Author Share Posted March 8 ahh ok. I get it. Thanks Barand Quote Link to comment Share on other sites More sharing options...
Barand Posted March 8 Share Posted March 8 Try something like... $urls = array_column($statement->fetchAll(), 'domain_names'); $tlds = array_map(fn($v)=>strstr($v, '.'), $urls); $counts = array_count_values($tlds); Quote Link to comment Share on other sites More sharing options...
Barand Posted March 8 Share Posted March 8 add this and job almost done... $tot = array_sum($counts); $pcents = array_map(fn($v)=>$v*100/$tot, $counts); Quote Link to comment Share on other sites More sharing options...
webdeveloper123 Posted March 8 Author Share Posted March 8 wow! What code. Thanks for all your help Barand! 😁 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.