2017-04-19 61 views
0

我想從我的json響應中檢索唯一國家/地區代碼的列表。不幸的是,我沒有任何運氣,我認爲我的代碼已經走上正軌。來自Json的PHP唯一值

我的JSON響應的樣子:

[ 
{ 
marketId: "1.131082455", 
marketName: "6f Hcap", 
marketStartTime: "2017-04-20T12:50:00.000Z", 
totalMatched: 4947.48, 
runners: [], 
eventType: {}, 
event: { 
id: "28198264", 
name: "Newm 20th Apr", 
countryCode: "GB", 
timezone: "Europe/London", 
venue: "Newmarket", 
openDate: "2017-04-20T12:50:00.000Z" 
} 
}, 
{ 
marketId: "1.131098324", 
marketName: "2m4f Listed", 
marketStartTime: "2017-04-20T13:05:00.000Z", 
totalMatched: 475.02, 
runners: [], 
eventType: { 
id: "7", 
name: "Horse Racing" 
}, 
event: { 
id: "28199254", 
name: "Chelt 20th Apr", 
countryCode: "GB", 
timezone: "Europe/London", 
venue: "Cheltenham", 
openDate: "2017-04-20T13:05:00.000Z" 
} 
}, 
{ 
marketId: "1.131098106", 
marketName: "5f Nov Stks", 
marketStartTime: "2017-04-20T13:15:00.000Z", 
totalMatched: 462.02, 
runners: [ 
{ 
selectionId: 12894664, 
runnerName: "Maggies Angel", 
handicap: 0, 
sortPriority: 1 
}, 
{ 
selectionId: 12894662, 
runnerName: "Beau Times", 
handicap: 0, 
sortPriority: 2 
}, 
{ 
selectionId: 12894669, 
runnerName: "Under Offer", 
handicap: 0, 
sortPriority: 3 
}, 
{ 
selectionId: 12894663, 
runnerName: "Che Bella", 
handicap: 0, 
sortPriority: 4 
}, 
{ 
selectionId: 12894666, 
runnerName: "Runthatbymeagain", 
handicap: 0, 
sortPriority: 5 
}, 
{ 
selectionId: 12894667, 
runnerName: "Societys Dream", 
handicap: 0, 
sortPriority: 6 
}, 
{ 
selectionId: 12894665, 
runnerName: "Mount Victoria", 
handicap: 0, 
sortPriority: 7 
}, 
{ 
selectionId: 12894668, 
runnerName: "St Helens Gate", 
handicap: 0, 
sortPriority: 8 
}, 
{ 
selectionId: 12894661, 
runnerName: "Autumn Belle", 
handicap: 0, 
sortPriority: 9 
} 
], 
eventType: { 
id: "7", 
name: "Horse Racing" 
}, 
event: { 
id: "28199240", 
name: "Ripon 20th Apr", 
countryCode: "GB", 
timezone: "Europe/London", 
venue: "Ripon", 
openDate: "2017-04-20T13:15:00.000Z" 
} 
}, 
{ 
marketId: "1.131082460", 
marketName: "7f Hcap", 
marketStartTime: "2017-04-20T13:25:00.000Z", 
totalMatched: 3215, 
runners: [ 
{ 
selectionId: 8632310, 
runnerName: "Salateen", 
handicap: 0, 
sortPriority: 1 
}, 
{ 
selectionId: 10200126, 
runnerName: "Tabarrak", 
handicap: 0, 
sortPriority: 2 
}, 
{ 
selectionId: 2835286, 
runnerName: "Accession", 
handicap: 0, 
sortPriority: 3 
}, 
{ 
selectionId: 6793501, 
runnerName: "Horsted Keynes", 
handicap: 0, 
sortPriority: 4 
}, 
{ 
selectionId: 10991108, 
runnerName: "Castle Harbour", 
handicap: 0, 
sortPriority: 5 
}, 
{ 
selectionId: 9483425, 
runnerName: "Qeyaadah", 
handicap: 0, 
sortPriority: 6 
}, 
{ 
selectionId: 10094829, 
runnerName: "Mix And Mingle", 
handicap: 0, 
sortPriority: 7 
} 
], 
eventType: { 
id: "7", 
name: "Horse Racing" 
}, 
event: { 
id: "28198264", 
name: "Newm 20th Apr", 
countryCode: "GB", 
timezone: "Europe/London", 
venue: "Newmarket", 
openDate: "2017-04-20T12:50:00.000Z" 
} 
}, 
{ 
marketId: "1.131098329", 
marketName: "2m5f Listed", 
marketStartTime: "2017-04-20T13:40:00.000Z", 
totalMatched: 2092.74, 
runners: [ 
{ 
selectionId: 8720215, 
runnerName: "Antartica De Thaix", 
handicap: 0, 
sortPriority: 1 
}, 
{ 
selectionId: 6879856, 
runnerName: "Magic Money", 
handicap: 0, 
sortPriority: 2 
}, 
{ 
selectionId: 9306372, 
runnerName: "Plaisir Damour", 
handicap: 0, 
sortPriority: 3 
}, 
{ 
selectionId: 8498571, 
runnerName: "Briery Belle", 
handicap: 0, 
sortPriority: 4 
}, 
{ 
selectionId: 10734687, 
runnerName: "Bagging Turf", 
handicap: 0, 
sortPriority: 5 
}, 
{ 
selectionId: 8409541, 
runnerName: "Storming Strumpet", 
handicap: 0, 
sortPriority: 6 
}, 
{ 
selectionId: 9022305, 
runnerName: "Timons Tara", 
handicap: 0, 
sortPriority: 7 
} 
], 
eventType: { 
id: "7", 
name: "Horse Racing" 
}, 
event: { 
id: "28199254", 
name: "Chelt 20th Apr", 
countryCode: "GB", 
timezone: "Europe/London", 
venue: "Cheltenham", 
openDate: "2017-04-20T13:05:00.000Z" 
} 
}, 
{ 
marketId: "1.131098111", 
marketName: "5f Nov Stks", 
marketStartTime: "2017-04-20T13:50:00.000Z", 
totalMatched: 174.22, 
runners: [ 
{ 
selectionId: 12844624, 
runnerName: "Nobrassnolass", 
handicap: 0, 
sortPriority: 1 
}, 
{ 
selectionId: 349630, 
runnerName: "Faithful Promise", 
handicap: 0, 
sortPriority: 2 
}, 
{ 
selectionId: 12894672, 
runnerName: "Flos Melody", 
handicap: 0, 
sortPriority: 3 
}, 
{ 
selectionId: 12894676, 
runnerName: "Showdancing", 
handicap: 0, 
sortPriority: 4 
}, 
{ 
selectionId: 12894674, 
runnerName: "Listen Alexander", 
handicap: 0, 
sortPriority: 5 
}, 
{ 
selectionId: 12894670, 
runnerName: "Bow Belles", 
handicap: 0, 
sortPriority: 6 
}, 
{ 
selectionId: 12894673, 
runnerName: "Kirbec", 
handicap: 0, 
sortPriority: 7 
}, 
{ 
selectionId: 12894675, 
runnerName: "Orient Princess", 
handicap: 0, 
sortPriority: 8 
}, 
{ 
selectionId: 12894671, 
runnerName: "Capla Dancer", 
handicap: 0, 
sortPriority: 9 
} 
], 
eventType: { 
id: "7", 
name: "Horse Racing" 
}, 
event: { 
id: "28199240", 
name: "Ripon 20th Apr", 
countryCode: "GB", 
timezone: "Europe/London", 
venue: "Ripon", 
openDate: "2017-04-20T13:15:00.000Z" 
} 
}, 
{ 
marketId: "1.131082465", 
marketName: "6f Grp 3", 
marketStartTime: "2017-04-20T14:00:00.000Z", 
totalMatched: 2111.62, 
runners: [ 
{ 
selectionId: 8556767, 
runnerName: "Brando", 
handicap: 0, 
sortPriority: 1 
}, 
{ 
selectionId: 7331634, 
runnerName: "Ornate", 
handicap: 0, 
sortPriority: 2 
}, 
{ 
selectionId: 7249496, 
runnerName: "Windfast", 
handicap: 0, 
sortPriority: 3 
}, 
{ 
selectionId: 5488936, 
runnerName: "Kassia", 
handicap: 0, 
sortPriority: 4 
}, 
{ 
selectionId: 464226, 
runnerName: "Aeolus", 
handicap: 0, 
sortPriority: 5 
}, 
{ 
selectionId: 9591578, 
runnerName: "Tasleet", 
handicap: 0, 
sortPriority: 6 
}, 
{ 
selectionId: 9507759, 
runnerName: "Mayfair Lady", 
handicap: 0, 
sortPriority: 7 
} 
], 
eventType: { 
id: "7", 
name: "Horse Racing" 
}, 
event: { 
id: "28198264", 
name: "Newm 20th Apr", 
countryCode: "GB", 
timezone: "Europe/London", 
venue: "Newmarket", 
openDate: "2017-04-20T12:50:00.000Z" 
} 
}, 
{ 
marketId: "1.131098334", 
marketName: "2m4f Listed", 
marketStartTime: "2017-04-20T14:15:00.000Z", 
totalMatched: 2146.8, 
runners: [ 
{ 
selectionId: 10839474, 
runnerName: "Snow Leopardess", 
handicap: 0, 
sortPriority: 1 
}, 
{ 
selectionId: 6919614, 
runnerName: "Indian Stream", 
handicap: 0, 
sortPriority: 2 
}, 
{ 
selectionId: 11208905, 
runnerName: "Carnspindle", 
handicap: 0, 
sortPriority: 3 
}, 
{ 
selectionId: 9289732, 
runnerName: "Midnight Tour", 
handicap: 0, 
sortPriority: 4 
}, 
{ 
selectionId: 8947698, 
runnerName: "Midnight Silver", 
handicap: 0, 
sortPriority: 5 
}, 
{ 
selectionId: 10462183, 
runnerName: "Theatre Territory", 
handicap: 0, 
sortPriority: 6 
}, 
{ 
selectionId: 11980162, 
runnerName: "Sparkling River", 
handicap: 0, 
sortPriority: 7 
}, 
{ 
selectionId: 8091258, 
runnerName: "Tara Mist", 
handicap: 0, 
sortPriority: 8 
}, 
{ 
selectionId: 9483267, 
runnerName: "The Organist", 
handicap: 0, 
sortPriority: 9 
}, 
{ 
selectionId: 11154117, 
runnerName: "Mariahs Legend", 
handicap: 0, 
sortPriority: 10 
}, 
{ 
selectionId: 9277428, 
runnerName: "Miss Tongabezi", 
handicap: 0, 
sortPriority: 11 
}, 
{ 
selectionId: 7728577, 
runnerName: "Hopes Wishes", 
handicap: 0, 
sortPriority: 12 
}, 
{ 
selectionId: 7629108, 
runnerName: "On Demand", 
handicap: 0, 
sortPriority: 13 
}, 
{ 
selectionId: 9254034, 
runnerName: "Miss Crick", 
handicap: 0, 
sortPriority: 14 
} 
], 
eventType: { 
id: "7", 
name: "Horse Racing" 
}, 
event: { 
id: "28199254", 
name: "Chelt 20th Apr", 
countryCode: "GB", 
timezone: "Europe/London", 
venue: "Cheltenham", 
openDate: "2017-04-20T13:05:00.000Z" 
} 
}, 
{ 
marketId: "1.131098116", 
marketName: "6f Hcap", 
marketStartTime: "2017-04-20T14:25:00.000Z", 
totalMatched: 1937.98, 
runners: [ 
{ 
selectionId: 9527523, 
runnerName: "Alkhor", 
handicap: 0, 
sortPriority: 1 
}, 
{ 
selectionId: 8534817, 
runnerName: "Dandyleekie", 
handicap: 0, 
sortPriority: 2 
}, 
{ 
selectionId: 8902727, 
runnerName: "Magical Effect", 
handicap: 0, 
sortPriority: 3 
}, 
{ 
selectionId: 8522672, 
runnerName: "Straightothepoint", 
handicap: 0, 
sortPriority: 4 
}, 
{ 
selectionId: 6482488, 
runnerName: "Salvatore Fury", 
handicap: 0, 
sortPriority: 5 
}, 
{ 
selectionId: 9987430, 
runnerName: "Rantan", 
handicap: 0, 
sortPriority: 6 
}, 
{ 
selectionId: 8837262, 
runnerName: "Gabrial The Tiger", 
handicap: 0, 
sortPriority: 7 
}, 
{ 
selectionId: 8652131, 
runnerName: "Eastern Racer", 
handicap: 0, 
sortPriority: 8 
}, 
{ 
selectionId: 7610360, 
runnerName: "Royal Connoisseur", 
handicap: 0, 
sortPriority: 9 
}, 
{ 
selectionId: 3186, 
runnerName: "Dragon King", 
handicap: 0, 
sortPriority: 10 
}, 
{ 
selectionId: 8722763, 
runnerName: "Honeysuckle Lil", 
handicap: 0, 
sortPriority: 11 
}, 
{ 
selectionId: 4299084, 
runnerName: "Best Trip", 
handicap: 0, 
sortPriority: 12 
}, 
{ 
selectionId: 10304529, 
runnerName: "Mywayistheonlyway", 
handicap: 0, 
sortPriority: 13 
}, 
{ 
selectionId: 9504726, 
runnerName: "Still On Top", 
handicap: 0, 
sortPriority: 14 
}, 
{ 
selectionId: 10174031, 
runnerName: "Taskeen", 
handicap: 0, 
sortPriority: 15 
} 
], 
eventType: { 
id: "7", 
name: "Horse Racing" 
}, 
event: { 
id: "28199240", 
name: "Ripon 20th Apr", 
countryCode: "GB", 
timezone: "Europe/London", 
venue: "Ripon", 
openDate: "2017-04-20T13:15:00.000Z" 
} 
}, 
{ 
marketId: "1.131082470", 
marketName: "1m Grp 3", 
marketStartTime: "2017-04-20T14:35:00.000Z", 
totalMatched: 10616.28, 
runners: [ 
{ 
selectionId: 11252663, 
runnerName: "War Decree", 
handicap: 0, 
sortPriority: 1 
}, 
{ 
selectionId: 11472950, 
runnerName: "Rivet", 
handicap: 0, 
sortPriority: 2 
}, 
{ 
selectionId: 11378900, 
runnerName: "Larchmont Lad", 
handicap: 0, 
sortPriority: 3 
}, 
{ 
selectionId: 12685204, 
runnerName: "Benbatl", 
handicap: 0, 
sortPriority: 4 
}, 
{ 
selectionId: 11576840, 
runnerName: "Eminent", 
handicap: 0, 
sortPriority: 5 
}, 
{ 
selectionId: 5443284, 
runnerName: "Gulliver", 
handicap: 0, 
sortPriority: 6 
}, 
{ 
selectionId: 11478374, 
runnerName: "Contrapposto", 
handicap: 0, 
sortPriority: 7 
} 
], 
eventType: { 
id: "7", 
name: "Horse Racing" 
}, 
event: { 
id: "28198264", 
name: "Newm 20th Apr", 
countryCode: "IE", 
timezone: "Europe/London", 
venue: "Newmarket", 
openDate: "2017-04-20T12:50:00.000Z" 
} 
} 
] 

和我所試圖做的是現在可以顯示獨特的「COUNTRYCODE」值(我知道是「國標」和「IE」不幸的是我,我的代碼是沒有做多少

這裏是我有什麼(對不起它不見了那種草率現在):

<?php 

$api_url = "http://localhost:8888/api/"; 


$jsondata = file_get_contents($api_url); 
$obj = json_decode($jsondata, true); 





if (is_array($obj)){ 

    echo "<table>"; 
    echo "<tr> 
      <th>Meeting</th> 

     </tr>"; 
    foreach ($obj as $row){ 

     $meeting = $row["marketNam"]; 
     $race_id = $row["marketId"]; 
     $venue = $row["event"]["venue"]; 
     $date = $row["event"]["openDate"]; 
     $countryCode = $row["event"]["countryCode"]; 

     $dateFormatted = date('m/d/Y H:i:s', $date); 


     $values = array_unique($countryCode, SORT_REGULAR); 


     echo "<tr>"; 
      echo "<td><a href='#'>".$values."</a></td>"; 
      //echo "<td><a href='races.php?id=".$race_id."&venue=".$meeting."'>".$venue."</a></td>"; 
     echo "</tr>"; 
    } 
    echo "</table>"; 
} 

?> 
+0

在你的代碼中,'$ countryCode'將是一個單一的值,而不是一個數組。 –

回答

2

在你的代碼,$countryCode將是一個值,而不是AR射線。我不知道你是用表格做什麼,但這裏有一對夫婦的方式來獲得的國家代碼唯一列表:

// iterate your data and add each item's country to a $countries array 
foreach ($obj as $row) { 
    $countries[] = $row['event']['countryCode']; 
} 
// use array_unique to remove duplicates 
$countries = array_unique($countries); 

// iterate your data and add each item's country to a $countries array 
foreach ($obj as $row) { 
    // using countryCode as the key will keep the array unique. 
    $countries[$row['event']['countryCode']] = true; 
} 
//swap the keys to values 
$countries = array_keys($countries); 

使用這些方法中的一種意味着在輸出任何東西之前你必須得到唯一的列表,然後遍歷結果列表輸出。