Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nomanshah's avatar

Php array find percentage

I have this array. i need to find percentage comparison between month . Mar 2020 of partner domain = 258(0.87%) and Mar 2020 of Non partner domain = 29242(99.1%) Please help me anyone Thanks

Array ( [Partner] => Array ( [Mar 2020] => Array ( [parameter] => Partner [domain] => 258 [month] => Mar 2020 ) [Apr 2020] => Array ( [parameter] => Partner [domain] => 2 [month] => Apr 2020 ) )

[Non Partner] => Array
    (
        [Mar 2020] => Array
            (
                [parameter] => Non Partner
                [domain] => 29242
                [month] => Mar 2020
            )
        [Apr 2020] => Array
            (
                [parameter] => Non Partner
                [domain] => 56
                [month] => Apr 2020
            )
    )

)

0 likes
2 replies
GeordieJackson's avatar

If your arrays have the same internal structure, you can loop through one and pull the corresponding values out of the other for the calculation.

Here's a quick example to get you started (assuming the arrays are named: $partner and $nonPartner):

$comparisons = [];

foreach($partner as $month => $data) {
    $difference = $data['domain'] / ($data['domain'] + $nonPartner[$month]['domain']) * 100;
    $comparisons[] = ['month' => $month, 'difference' => number_format($difference, 2)];
}

Please or to participate in this conversation.