douci7521 2016-03-29 15:08
浏览 27
已采纳

比较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'

  • 写回答

1条回答 默认 最新

  • douyun7285 2016-03-29 15:43
    关注

    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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题