2013-08-28 71 views
0

下面是我的代碼中數組的格式,完整代碼和數組內容的兩個示例。搜索多維數組以匹配兩個數組的鍵與Php

陣列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鍵找到匹配的值。然後我需要將兩個數組的值組合成一個數組,以便我可以將它們打印在屏幕上。提供了兩個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>"; 
+0

顯示結果數組應該是什麼樣子。 –

+0

是否要打印所有玩家和打印傷害(如果有的話),還是隻打印受傷的玩家? – migg

+0

我需要一個新的數組,其ID爲球員[名字]鍵,顯示球隊的全名。這是一個運動損傷API,所以我想顯示所有受傷的球員。實際的plaers名稱在另一個API調用中,如下所示:http://football.myfantasyleague.com/2012/export?TYPE=players&PLAYERS=10067&JSON=1我需要在這裏做一些事情:&players = [Id key goes這裏]我的腦子疼... –

回答

1

好吧,我假設你想添加受傷的球員。輸出將是球員名單,與傷病加(它們適用於其中)

$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

編輯的最新的工作版本:

哦,你使用的是PHP 5.2。這裏是一個PHP 5.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

+0

解析錯誤:語法錯誤,意想不到的T_FUNCTION,期待')'在17行/home4/bags/public_html/wp-content/themes/fantasy/page.php不知道這是不是或者不行,但第17行是第一行代碼。我的服務器運行Php 5.2,有沒有辦法做到這一點,而沒有匿名功能?我不知道我在這裏錯過了什麼,代碼看起來不錯,我就像你寫它但不工作:( –

+0

@MichaelHicks這是你使用PHP 5.2的恥辱,但無論如何,我已經創建了一個PHP 5.2兼容版本。請檢查它 –

+0

我沒有得到它當我結合了一切時,我得到了什麼:http://codepad.org/fD2ym6Yo當我在mky網站上嘗試時,我得到這個錯誤:Warning:array_filter()[function.array -filter]:第一個參數應該是第16行的/home4/bags/public_html/wp-content/themes/fantasy/page.php中的一個數組。 –