根据共享的id值将数据从一个多维数组映射到另一个多维阵列


Map data from one multidimensional array to another based on shared id value

以下是数组格式的两个示例,完整代码和下面代码中的数组内容。

阵列1

[
    'version' => '1.0',
    'injuries' => [
        'timestamp' => 1377702112,
        'week' => 1,
        'injury' => [
            [
                'status' => 'Questionable',
                'id' => '10009',
                'details' => 'Shoulder'
            ],
            [
                'status' => 'Questionable',
                'id' => '10012',
                'details' => 'Ankle'
            ]
        ]
    ]    
]

阵列2

[
    'version' => '1.0',
    'players' => [
        'timestamp' => 1377676937,
        'player' => [
            [
                'position' => 'TMDL',
                'name' => 'Bills, Buffalo',
                'id' => '0251',
                'team' => 'BUF'
            ],
            [
                'position' => 'TMDL',
                'name' => 'Colts, Indianapolis',
                'id' => '10009',
                'team' => 'IND'
            ]
        ]
    ]
]

我需要做的是对这两个数组进行排序,并为ID键找到匹配的值。然后我需要将两个数组的值组合成一个数组,这样我就可以在屏幕上打印出来。提供了两个API,一个用于带有球员[id]密钥的伤病报告,另一个用于带[id]键的球员信息。以下是我在这个问题上的进展:

<?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>

这是工作代码,感谢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>";
     

好吧,我假设你想给球员增加伤害。输出将是球员名单,并添加伤病(在他们适用的地方)

$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);

输出是这样的:

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

编辑最新工作版本:

哦,您使用的是php5.2。这是一个php5.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);

$array1在这里是全局的。请在此处查看:http://pastebin.com/N3RqtfzN