比较API的多个数组输出

比较API的多个数组输出

问题描述:

I'm currently building a new tool using an API provided by Troy Hunt from his site Have I been pwned?

The output from the API address provides an easy to search through JSON array for a breach but within each array there is another array called 'DataClasses' which may contain such values as 'Email addresses' or 'Usernames'. Sometimes when a user is caught in more than one breach there is obviously more than one 'DataClasses' array to go through.

My question is if there is more than one array, how would I compare each one to find out what they have in common. So I could for example output 'We found in Email addresses in 3 breaches' or something similar. How could I do this? The for loop I use is as shown:

$dc = count($fs[$i]['DataClasses']);
for($j=0;$j<$dc;$j++) {
   $datclass = $fs[$i]['DataClasses'][$j];
}

Anyone have any ideas? Just in case anyone asks, yes that is a for loop within a for loop as like I said 'DataClasses' is a an array within the original JSON array that is output, so just to clarify an example would be:

['DataClasses'][0]

could equal 'Email addresses'

Like so?

$fs = json_decode($jsonString, true);
$dc = array_count_values(call_user_func_array('array_merge', array_map(function($x) {return $x['DataClasses'];}, $fs)));
var_dump($dc);

Output:

array(7) {
  ["Email addresses"]=>
  int(3)
  ["IP addresses"]=>
  int(1)
  ["Names"]=>
  int(1)
  ["Passwords"]=>
  int(3)
  ["Password hints"]=>
  int(1)
  ["Usernames"]=>
  int(2)
  ["Dates of birth"]=>
  int(1)
}