Search code examples
phparraysnumeric

Iterate and count unique values from array with numerical index?


I have the following array:

Array
(
    [0] => Array
        (
            [url] => https://website1.com/
            [remote_address] => Array
                (
                    [ip] => 1.1.1.1
                    [port] => 443
                )

        [headers] => Array
            (
                [date] => Mon, 31 Jan 2022 11:16:30 GMT
                [content-type] => text/html
            )

    )

[1] => Array
    (
        [url] => https://www.website1.com/
        [remote_address] => Array
            (
                [ip] => 1.1.1.1
                [port] => 443
            )

        [headers] => Array
            (
                [date] => Mon, 31 Jan 2022 11:16:30 GMT
                [content-encoding] => gzip
            )

    )

[2] => Array
    (
        [url] => https://www.website2.com/
        [remote_address] => Array
            (
                [ip] => 2.2.2.2
                [port] => 443
            )

        [headers] => Array
            (
                [date] => Mon, 31 Jan 2022 11:16:30 GMT
                [content-encoding] => br
            )

    )

[3] => Array
    (
        [url] => https://www.website3.com/
        [remote_address] => Array
            (
                [ip] => 3.3.3.3
                [port] => 443
            )

        [headers] => Array
            (
                [date] => Mon, 31 Jan 2022 11:16:30 GMT
                [content-encoding] => br
            )

    )

[4] => Array
    (
        [url] => https://www.website2.com/
        [remote_address] => Array
            (
                [ip] => 2.2.2.2
                [port] => 443
            )

        [headers] => Array
            (
                [date] => Mon, 31 Jan 2022 11:16:30 GMT
                [content-encoding] => gzip
            )

    )

[5] => Array
    (
        [url] => https://www.website4.com/
        [remote_address] => Array
            (
                [ip] => 4.4.4.4
                [port] => 443
            )

        [headers] => Array
            (
                [date] => Mon, 31 Jan 2022 10:44:46 GMT
                [content-encoding] => gzip
            )

    )
)

Using PHP (7.4) how can could I interate the array, find all IP:s and then count unique IP:s that is not the first IP in the array? Right know I'm kind of lost when trying to iterate and maybe that I'm thinking the array i multidimensional?

The "mother" IP that should not be included in the count could be fetched outside this array as an variable.

Any help appreciated, thanks.


Solution

  • So your problem appears to be you dont know how to iterate over an array.

    A simple foreach loop would suffice here

    // init the array to hold the ips and their counts.
    $ips = [];
    
    foreach( $bigArray as $inner ) {
        $ip = $inner['remote_address']['ip']
        if ( array_key_exists($ip, $ips) ) {
            // we saw this IP before, so add 1 to count
            $ips[$ip]['count']++;
        } else {
            // first time we saw this ip, create entry in ips array with count of zero
            $ips[$ip] = ['count' => 0];
        }
    }
    print_r($ips);
    

    This copes with not counting the first occurance of the ip, if you want to count that one also just start the counter off at 1 instead of 0.