0

配列の形式の 2 つの例、完全なコード、および以下のコードの配列コンテンツを次に示します。

配列 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

)

配列 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
)

私がする必要があるのは、両方の配列を並べ替えて、ID キーの一致する値を見つけることです。次に、両方の配列の値を 1 つの配列に結合して、画面に出力できるようにする必要があります。2 つの API が提供されています。1 つはプレーヤーの [id] キーを持つ負傷レポート用で、もう 1 つは [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>

クリスのおかげで、ここに作業コードがあります:)

$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>";
4

1 に答える 1