0

This is the array.

Array
        (
            [0] => Array
                (
                    [position] => TMDL
                    [name] => Bills, Buffalo
                    [id] => 0251
                    [team] => BUF
                )
    
            [290] => Array
                (
                    [position] => TMDL
                    [name] => Colts, Indianapolis
                    [id] => 0252
                    [team] => IND
                )
    
            [395] => Array
                (
                    [position] => TMDL
                    [name] => Dolphins, Miami
                    [id] => 0253
                    [team] => MIA
                )
            [482] => Array
                (
                    [position] => CB
                    [name] => Hall, Deangelo
                    [id] => 7398
                    [team] => WAS
                    [status] => Probable
                    [details] => Ankle
    
                )
        )

What I am trying to do is only show the contents of the 2d array that have the injury items like [status] and [details] because some of them only have the [position] [name] [id] and [team] keys. Below is my code that I have come up with so far but it prints everything in the Array. I tried array_key_exists in the array loop but I'm not sure I know what I'm doing with it.

$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 "<ul>";
             $result = array();
              foreach( $output as $key => $category ) {
                 foreach( $category as $index => $value ) {
                     $result[$index][$key]= $value;
                                          
                     echo "<li>" . $value . "</li>" ;                     
                     
               } 
             }
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Kustom
  • 181
  • 3
  • 15
  • I have rolled this question back to its original version. Please never edit your question to correct your code. After you edited, it made your question seem nonsensical and will leave researchers scratching their head as to how the answers resolve the seemingly unbroken code. – mickmackusa Sep 04 '22 at 21:36

2 Answers2

0

If Im reading this right, in your map function you could do this:

if(!array_key_exists('status', $x))
    return;

Which would return before doing anything IF the input '$x' does not have a 'status' key.

Michael Wheeler
  • 670
  • 7
  • 11
0

Add the search on the $category to display only array items that have details (as in details on the injury):

    if (array_key_exists("details", $category)) {
      foreach( $category as $index => $value ) {
        $result[$index][$key]= $value;
        echo "<li>" . $value . " </li>" ;
      } 
    }
KC135Q
  • 44
  • 4