Hello to all! The task seemed trivial, but for me it turned out to be complicated.

Let's report for a certain range of dates. I did not bother much, and I have it in the form of an associative array. The list of array keys is always the same, only their values ​​differ.

Are there any libraries for facilitating the display of such arrays standing next to each other, and at the same time creating a third column in which the difference in indicators is displayed?

The result should be like this: alt text Yellow color indicates values ​​from two arrays, and red - the difference.

I built my bike, but it is not very beautiful, and therefore asked this question.

Also interested in the question of comparing two arrays with a variable set of keys.

Glad to hear any thoughts on the topic:]

  • and what prevents one request to get all the necessary data for the report? - Ale_x
  • and it has to do with it? the question is not how to build a report, but how to visualize 2 sets of data already ready - nolka
  • Then the question is not correct and where is the "visual comparison"? This is called inference. Take and display in tabular form. - Artem
  • I need to display in a column, not in a row) - nolka

2 answers 2

If I understand the question correctly, then the visualization can be organized as follows: first get a list of all keys, and then work with both arrays at once.

$keys = array_keys($period1); foreach($keys as $key) { $diff = $period2[$key] - $period1[$key]; echo "{$key} | {@$period1[$key]} | {@$period2[$key]} | {$diff}"; } 
  • Fuh, then I’m not the only one like this:] Tell me, what about when two arrays have different sets of keys? Moreover, it may be that the number of keys in both arrays is the same, but they are different) - nolka
  • one
    You can try to combine the keys $ keys = array_unique (array_merge ($ period1, $ period2)); Then, in the absence of any value in one of the periods, there will be something like + -------------------- + ---- + ---- + ---- + | Indicators | P1 | P2 | R | + -------------------- + ---- + ---- + ---- + | Gopnik slain | 4 | 0 | -4 | | Hired Killers | 2 | 3 | 1 | | Banned stupid | 0 | 2 | 2 | | Destroyed by trolls | 0 | 1 | 1 | + -------------------- + ---- + ---- + ---- + - Nikolai Kim
  • @tabby_cat, thank you for it) - nolka

Look towards these functions:

array_intersect

array_diff

  • Read the question carefully. i need to visualize - nolka
  • AND? Output one array, next to the second array, next to the result array_diff / intersect. - user6550
  • I updated my question - nolka
  • Only the picture has no relation to the question. - user6550
  • What is it? The question clearly states that you need to display on the screen, and next to display the difference in the values ​​of these indicators. - nolka