0

Here are two example of the format of the Arrays, full code and array content in my code below.

ARRAY 1

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

ARRAY 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'
            ]
        ]
    ]
]

What I need to do is sort through both Arrays and finding matching values for the ID key. 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. 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>";
     
3
  • Show how result array should look like. Commented Aug 28, 2013 at 18:14
  • Do you want to print all players and print injuries if the have any, or do you only want to print out injured players? Commented Aug 28, 2013 at 18:26
  • I need a new array with the matching ID's players [name] key that shows the full name of the team. This is a sports injuries API so I want to show all injured players. The actual plaers name is in another API call that looks like this: football.myfantasyleague.com/2012/… I'll need to do something here: &players=[Id key goes here] my brain hurts... Commented Aug 28, 2013 at 18:42

1 Answer 1

1

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

Edit The latest working version:

Oh you are using php 5.2. Here is a php 5.2 version, but it less pretty than the code before:

$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. Check it here: http://pastebin.com/N3RqtfzN

Sign up to request clarification or add additional context in comments.

10 Comments

Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in /home4/bags/public_html/wp-content/themes/fantasy/page.php on line 17 Not sure if this is it or not but line 17 is the first line of code. My server is running Php 5.2, is there a way to do it without an anonymous function? Im not sure what Im missing here, the code looks good, I have it just like you wrote it but not working:(
@MichaelHicks It's a shame you are using php 5.2, but anyway, I have created a php 5.2 compatible version. Please check it.
I dont get it. Heres what I get when I combine everything: codepad.org/fD2ym6Yo Also when I try it on mky website I get this error: Warning: array_filter() [function.array-filter]: The first argument should be an array in /home4/bags/public_html/wp-content/themes/fantasy/page.php on line 16
You used the C option instead of the php one.
anyway, try this one: pastebin.com/hKuJvWPQ (I didn't used codepad.org here, since it seems it does not like the file_get_contents command)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.