Question

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) {
    if (isset($category['status'])) {
        foreach ($category as $index => $value) {
            $result[$index][$key] = $value;

            echo "<li>" . $value . "</li>";
        }
    }
}
echo "</ul>";
Was it helpful?

Solution

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>" ;
      } 
    }

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top