0

I have two arrays with the year's results.

array A (   
[a] => '150'  
[b] => '200'
[c] => '300'
[d] => '1000'
[e] => '350'
[f] => '1000'
)

array B (
[a] => '500'  
[b] => '400'
[d] => '1000'
[f] => '1000'
)

I need to compare the growth results between the two building another array, to show it in a html table. Ex:

[a] => 233%  
[b] => 100%
...

I have a array identifying the indexes that are not present on array b. array c = ('c', 'e');

The thing is, I need the row C and E to still be displayed on the table. But on the iteration, how can i just jump the line with this indexes that have 0 value avoiding calculation 300 by 0 and putting a message instead?

5
  • 1
    Many ways, have you tried using a while loop and implementing a condition to compare an array value against another arrays value with the same index key? Commented Nov 22, 2018 at 18:20
  • 1
    1. Write a function that takes the two arrays as arguments, loops through them, and returns the array of raw results of $a[x] / $b[x] 2. Write your display logic that takes this array and turns it into HTML according to whatever rules you want to define. 3. Remember that the results of division are floating point numbers, and you cannot reliably compare floats for equivalence. 4. if( abs($c[x] < .00001 ) { echo 'it is zero...ish'; } or whatever suitably small fraction that you find acceptable. Commented Nov 22, 2018 at 18:21
  • Actually I wrote it wrong. The thing is, index C and E isn't present at array B, I will update the post Commented Nov 22, 2018 at 18:24
  • 1
    does "array A" always contian all the keys necessary for comparison? Commented Nov 22, 2018 at 18:46
  • @gomd actually it can vary each year Commented Nov 22, 2018 at 18:48

3 Answers 3

1

You can iterate the first array and check the next one values:

$arrayA = [
    'a' => '150', 
    'b' => '200',
    'c' => '300',
    'd' => '1000',
    'e' => '350',
    'f' => '1000',
];

$arrayB = [
    'a' => '500', 
    'b' => '400',
    'd' => '1000',
    'f' => '1000',
];

$result = [];
foreach ($arrayA as $key => $value) {
    if(isset($arrayB[$key])) {
        $result[$key] = round($arrayB[$key] * 100 / $value, 2);
    } else {
        $result[$key] = 'some value when empty';
    }
}

var_dump($result);

Output:

array(6) {
  ["a"]=>
  float(333.33)
  ["b"]=>
  float(200)
  ["c"]=>
  string(21) "some value when empty"
  ["d"]=>
  float(100)
  ["e"]=>
  string(21) "some value when empty"
  ["f"]=>
  float(100)
}
Sign up to request clarification or add additional context in comments.

Comments

1

You could loop through array A and check if the key of array A exists in array B using array_key_exists and then calculate the growth percentage:

$arrayA = [
    'a' => '150',
    'b' => '200',
    'c' => '300',
    'd' => '1000',
    'e' => '350',
    'f' => '1000'
];

$arrayB = [
    'a' => '500',
    'b' => '400',
    'd' => '1000',
    'f' => '1000'
];

$arrayC = [];

foreach($arrayA as $keyA => $valueA) {
    if (array_key_exists($keyA, $arrayB)) {
        $arrayC[$keyA] = floor((($arrayB[$keyA] - $valueA) / $valueA ) * 100) . "%";
        continue;
    }
    $arrayC[$keyA] = "No match";
}

Result

Array
(
    [a] => 233%
    [b] => 100%
    [c] => No match
    [d] => 0%
    [e] => No match
    [f] => 0%
)

Demo

1 Comment

It worked too, thank you for your contribution and your time!
1

If you want a more flexible solution where you can check for both values of the two arrays (i.e. do not base the comparison keys on a single array) and have the possibility to expand it for more than two arrays (it may be useful to others who do not have your same goal).

Fetch the keys of the two array with array_keys and use array_unique in order to avoid duplicate keys values.

<?php
$array_a = [  
    'a' => 150,
    'b' => 200,
    'c' => 300,
    'd' => 1000,
    'e' => 350,
    'f' => 1000
];

$array_b = [
    'a' => 500, 
    'b' => 400,
    'd' => 1000,
    'f' => 1000
];

$keys_a = array_keys($array_a);
$keys_b = array_keys($array_b);

$keys = array_unique(array_merge($keys_a, $keys_b));

$result = [];
foreach ($keys as $key)
{
    if (isset($array_a[$key]) && isset($array_b[$key]))
    {
        $result[$key] = round((($array_b[$key] - $array_a[$key]) / $array_a[$key]) * 100);
    }
    else
    {
        $result[$key] = "missing key in one of the two arrays";
    }
}

Output:

Array
(
    [a] => 233
    [b] => 100
    [c] => missing key in one of the two arrays
    [d] => 0
    [e] => missing key in one of the two arrays
    [f] => 0
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.