2016-06-16 80 views
0

我有URL JSON陣列www.example.com/json.php搜索JSON字符串數組與PHP關鍵字

[ {"name": "Afghanistan", "code": "AF"}, {"name":"ÅlandIslands","code": "AX"}, {"name": "Albania", "code": "AL"}, {"name": "Algeria", "code": "DZ"}, {"name": "American Samoa", "code": "AS"}, {"name": "AndorrA", "code": "AD"}, {"name": "Angola", "code": "AO"}, {"name": "Anguilla", "code": "AI"}, {"name": "Antarctica", "code": "AQ"}, {"name": "Antigua and Barbuda", "code": "AG"}, {"name": "Argentina", "code": "AR"}, {"name": "Armenia", "code": "AM"}, {"name": "Aruba", "code": "AW"}, {"name": "Australia", "code": "AU"}, {"name": "Austria", "code": "AT"}, {"name": "Azerbaijan", "code": "AZ"}, {"name": "Bahamas", "code": "BS"}, {"name": "Bahrain", "code": "BH"}, {"name": "Bangladesh", "code": "BD"}, {"name": "Barbados", "code": "BB"}, {"name": "Belarus", "code": "BY"}, {"name": "Belgium", "code": "BE"}, {"name": "Belize", "code": "BZ"}, {"name": "Benin", "code": "BJ"}, {"name": "Bermuda", "code": "BM"}, {"name": "Bhutan", "code": "BT"}, {"name": "Bolivia", "code": "BO"}, {"name": "Bosnia and Herzegovina", "code": "BA"}, {"name": "Botswana", "code": "BW"}, {"name": "Bouvet Island", "code": "BV"}, {"name": "Brazil", "code": "BR"}, {"name": "British Indian Ocean Territory", "code": "IO"}, {"name": "Brunei Darussalam", "code": "BN"}, {"name": "Bulgaria", "code": "BG"}, {"name": "Burkina Faso", "code": "BF"}, {"name": "Burundi", "code": "BI"}, {"name": "Cambodia", "code": "KH"}, {"name": "Cameroon", "code": "CM"}, {"name": "Canada", "code": "CA"}, {"name": "Cape Verde", "code": "CV"}, {"name": "Cayman Islands", "code": "KY"}, {"name": "Central African Republic", "code": "CF"}, {"name": "Chad", "code": "TD"}, {"name": "Chile", "code": "CL"},{"name": "China", "code": "CN"}, {"name": "Christmas Island", "code": "CX"}, {"name": "Cocos (Keeling) Islands", "code": "CC"}, {"name": "Colombia", "code": "CO"}, {"name": "Comoros", "code": "KM"}, {"name": "Congo", "code": "CG"}, {"name": "Congo, The Democratic Republic of the", "code": "CD"}, {"name": "Cook Islands", "code": "CK"}, {"name": "Costa Rica", "code": "CR"}, {"name": "Cote D\"Ivoire", "code": "CI"}, {"name": "Croatia", "code": "HR"}, {"name": "Cuba", "code": "CU"}, {"name": "Cyprus", "code": "CY"}, {"name": "Czech Republic", "code": "CZ"},{"name": "Denmark", "code": "DK"}, {"name": "Djibouti", "code": "DJ"}, {"name": "Dominica", "code": "DM"}, {"name": "Dominican Republic", "code": "DO"}, {"name": "Ecuador", "code": "EC"}, {"name": "Egypt", "code": "EG"}, {"name": "El Salvador", "code": "SV"}, {"name": "Equatorial Guinea", "code": "GQ"}, {"name": "Eritrea", "code": "ER"}, 

如何使用關鍵字查詢篩選數據? 例子是這樣的:

的所有數據:https://restcountries.eu/rest/v1/all

搜索關鍵詞中:https://restcountries.eu/rest/v1/name/nesia

回答

-2

你必須讀取URL轉換爲字符串。然後,你應該使用json_decode()PHP函數(如果這是你的意思是關鍵字)。

可以讀取URL與curl命令:

$fp = fopen("curl_error.txt", "w"); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://osteohickson.com/a/misc/rest_copy/"); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_VERBOSE, 1); 
curl_setopt($ch, CURLOPT_STDERR, $fp); 
$result = curl_exec($ch); 

var_dump(json_decode($result)); 

curl_close($ch); 
fclose($fp); 

然而,網址: 「https://restcountries.eu/rest/v1/all」 是當我嘗試它禁止。這意味着您需要用戶名和密碼才能訪問數據。由於我沒有它,我在另一臺服務器上製作了一個字符串的副本用於測試目的。

現在您可以「獲取」數據,您想要檢查如何處理它。

+0

可以顯示的示例代碼? – rif

0

首先解碼的JSON字符串:

$property = 'name'; 
$keyword = 'nesia'; 

$result = array_filter($countries, function($country) use ($property, $keyword) { 
    return strpos($country->$property, $keyword) !== false; 
}); 

回調使用strpos將檢查每個項目的名稱的關鍵詞:

$countries = json_decode($json); 

然後你使用array_filter所產生的陣列可以過濾。請務必使用嚴格比較(!==而不是!=),這樣名稱(索引0)開頭的匹配不會被排除。

+0

我得到空白數組[] – rif

+0

如果你用這個確切的代碼使用你的問題中顯示的示例數組,那是因爲該數組中沒有名稱包含「nesia」。 –

0

下面的代碼將通過名稱篩選JSON數據:

$term = 'nesia'; 

$data = json_decode($json); 
$filtered = array_filter($data, function($item) use ($term) { 
    return stripos($item->name, $term) !== false; 
}); 

print_r($filtered); 
+0

您必須在回調中使用($ term)',否則將在該範圍內未定義。 –

+0

哎呀,謝謝!我糾正了代碼片段。 –