简体   繁体   English

搜索多维数组以用Php匹配两个数组的键

[英]Searching Multidimentional arrays to match keys of two arrays with Php

Here are two example of the format of the Arrays, full code and array content in my code below. 这是数组格式的两个示例,下面是我的代码中的完整代码和数组内容。

ARRAY 1 阵列1

Array
(
[version] => 1.0
[injuries] => Array
(
[timestamp] => 1377702112
[week] => 1
[injury] => Array
(
[0] => Array
(
[status] => Questionable
[id] => 10009
[details] => Shoulder

)

[1] => Array
(
[status] => Questionable
[id] => 10012
[details] => Ankle

)

ARRAY 2 阵列2

Array
(
[version] => 1.0
[players] => Array
(
[timestamp] => 1377676937
[player] => Array
(
[0] => Array
(
[position] => TMDL
[name] => Bills, Buffalo
[id] => 0251
[team] => BUF
)

[1] => Array
(
[position] => TMDL
[name] => Colts, Indianapolis
[id] => 10009
[team] => IND
)

What I need to do is sort through both Arrays and finding matching values for the ID key. 我需要做的是对两个数组进行排序并找到ID密钥的匹配值。 I need to then combine the values of both arrays into one array so I can print it out on screen. 然后,我需要将两个数组的值合并为一个数组,以便可以在屏幕上打印出来。 There is two API's provided, one for the injuries report with a player [id] key and the other for the Players Information with [id] keys. 提供了两种API,一种用于使用球员[id]键的伤病报告,另一种用于使用[id]键的运动员信息。 Here is how far I have gotten on this problem: 这是我在这个问题上得到的成果:

<?php

       function injuries_report() {         

       //Get json files

         $injuryData = file_get_contents('http://football.myfa...=1&callback=');
         $playerData = file_get_contents('http://football.myfa...L=&W=&JSON=1');

       //format json data into an array

          $obj1 = json_decode($injuryData, true);
         $obj2 = json_decode($playerData, true);



         //return print_r($obj1);   //print obj1 to see output

         return print_r($obj2);     //print obj2 to see output

       }

      ?> 

       <!--get injuries report -->

      <pre><?php injuries_report(); ?></pre>

Here's the working code, thanks to Chris:) 这是工作代码,感谢克里斯:)

$injuryData = file_get_contents('http://football.myfantasyleague.com/2013/export?TYPE=injuries&L=&W=&JSON=1&callback=');
    $array1 = json_decode($injuryData, true);
    $playerData = file_get_contents('http://football.myfantasyleague.com/2013/export?TYPE=players&L=&W=&JSON=1');
    $array2 = json_decode($playerData, true);

    function map($x) {
       global $array1;
       if(isset($x['id'])) {
          $id = $x['id'];
          $valid = array_filter($array1['injuries']['injury'], create_function('$injury', 'return $injury["id"] == "' . $id .'";'));
          if(count($valid) > 0) {
             $x = array_merge($x, array_shift($valid));
          }
           }
           return $x;
        }
        $output = array_map('map', $array2['players']['player']);
        echo "<pre>";
        print_r($output);
        echo "</pre>";

Ok, I'm assuming you want to add injuries to players. 好的,我假设您想对球员造成伤害。 The output will be the list of players, with the injuries added (where they apply) 输出将是球员列表,并添加伤害(在适用情况下)

$output = array_map(function($x) use ($array1) {
   $id = $x['id'];
   $valid = array_filter($array1['injuries']['injury'], function($injury) use ($id) {
      return $injury['id'] == $id;
   });
   if(count($valid) > 0) {
      $x = array_merge($x, $valid[0]);
   }
   return $x;
}, $array2['players']['player']);

print_r($output);

The output is this: 输出是这样的:

Array
(
    [0] => Array
        (
            [position] => TMDL
            [name] => Bills, Buffalo
            [id] => 0251
            [team] => BUF
        )

    [1] => Array
        (
            [position] => TMDL
            [name] => Colts, Indianapolis
            [id] => 10009
            [team] => IND
            [status] => Questionable
            [details] => Shoulder
        )
)

php 5.2 PHP 5.2

Edit The latest working version: 编辑最新的工作版本:

Oh you are using php 5.2. 哦,您使用的是php 5.2。 Here is a php 5.2 version, but it less pretty than the code before: 这是php 5.2版本,但是比以前的代码要漂亮:

$injuryData = file_get_contents('http://football.myfantasyleague.com/2013/export?TYPE=injuries&L=&W=&JSON=1&callback=');
$array1 = json_decode($injuryData, true);
$playerData = file_get_contents('http://football.myfantasyleague.com/2013/export?TYPE=players&L=&W=&JSON=1');
$array2 = json_decode($playerData, true);

function map($x) {
   global $array1;
   if(isset($x['id'])) {
      $id = $x['id'];
      $valid = array_filter($array1['injuries']['injury'], create_function('$injury', 'return $injury["id"] == "' . $id .'";'));
      if(count($valid) > 0) {
         $x = array_merge($x, array_shift($valid));
      }
   }
   return $x;
}
$output = array_map('map', $array2['players']['player']);
print_r($output);

The $array1 is global here. $array1在这里是全局的。 Check it here: http://pastebin.com/N3RqtfzN 在这里检查: http : //pastebin.com/N3RqtfzN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM