Jump to content

Using explode but only last string value being added to array


Go to solution Solved by Barand,

Recommended Posts

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.