0
好日子人,我的問題是這樣的,我有一個json格式文件,我用php的json_decode()
從這個json文件解碼我想迭代通過它並將所有的定義元素存儲在我將在我的函數結束時返回的變量中。我試過我如何返回數組中的函數內部的foreach循環中的值
function dictSearch(){
$file = file_get_contents('wiki.json');
$input = json_decode($file);
$ans = '';
$error = 'No definition for that word';
foreach ($input->results as $result){
foreach ($result as $value=>$key){
if ($value === 'definition'){
$answer[] = $key.'\n';
}
}
}
return print $answer;
}
但它沒有工作。 看看json文件。
{
"word": "rat",
"results": [
{
"definition": "someone who works (or provides workers) during a strike",
"partOfSpeech": "noun",
"synonyms": [
"blackleg",
"scab",
"strikebreaker"
],
"typeOf": [
"worker"
]
},
{
"definition": "a person who is deemed to be despicable or contemptible",
"partOfSpeech": "noun",
"synonyms": [
"bum",
"crumb",
"dirty dog",
"git",
"lowlife",
"puke",
"rotter",
"scum bag",
"skunk",
"so-and-so",
"stinker",
"stinkpot"
],
"typeOf": [
"unpleasant person",
"disagreeable person"
],
"examples": [
"kill the rat"
]
},
{
"definition": "take the place of work of someone on strike",
"partOfSpeech": "verb",
"synonyms": [
"blackleg",
"fink",
"scab"
],
"typeOf": [
"do work",
"work"
],
"derivation": [
"ratter"
]
},
{
"definition": "give away information about somebody",
"partOfSpeech": "verb",
"synonyms": [
"betray",
"denounce",
"give away",
"grass",
"shit",
"shop",
"snitch",
"stag",
"tell on"
],
"typeOf": [
"inform"
],
"hasTypes": [
"sell someone out"
],
"derivation": [
"ratting",
"ratter"
]
},
{
"definition": "one who reveals confidential information in return for money",
"partOfSpeech": "noun",
"synonyms": [
"betrayer",
"blabber",
"informer",
"squealer"
],
"typeOf": [
"source",
"informant"
],
"hasTypes": [
"nark",
"grass",
"fink",
"copper's nark",
"canary",
"sneak",
"sneaker",
"snitch",
"snitcher",
"stool pigeon",
"stoolie",
"stoolpigeon",
"supergrass"
]
},
{
"definition": "any of various long-tailed rodents similar to but larger than a mouse",
"partOfSpeech": "noun",
"typeOf": [
"rodent",
"gnawer"
],
"hasTypes": [
"oryzomys palustris",
"mole rat",
"norway rat",
"brown rat",
"rice rat",
"rattus rattus",
"rattus norvegicus",
"roof rat",
"bandicoot rat",
"black rat",
"pocket rat",
"jerboa rat"
],
"derivation": [
"ratty"
]
},
{
"definition": "a pad (usually made of hair) worn as part of a woman's coiffure",
"partOfSpeech": "noun",
"typeOf": [
"pad"
],
"partOf": [
"hairdo",
"coif",
"coiffure",
"hair style",
"hairstyle"
]
},
{
"definition": "catch rats, especially with dogs",
"partOfSpeech": "verb",
"typeOf": [
"capture",
"catch"
],
"derivation": [
"ratter"
]
},
{
"definition": "desert one's party or group of friends, for example, for one's personal advantage",
"partOfSpeech": "verb",
"typeOf": [
"desert",
"defect"
],
"derivation": [
"ratter"
]
},
{
"definition": "employ scabs or strike breakers in",
"partOfSpeech": "verb",
"inCategory": [
"manufacture",
"industry"
],
"typeOf": [
"hire",
"engage",
"employ"
],
"derivation": [
"ratter"
]
},
{
"definition": "give (hair) the appearance of being fuller by using a rat",
"partOfSpeech": "verb",
"typeOf": [
"fill out",
"pad"
]
}
],
"syllables": {
"count": 1,
"list": [
"rat"
]
},
"pronunciation": {
"all": "ræt"
},
"frequency": 4.49
}
我想要做的就是通過JSON文件迭代和定義的所有值存儲在其中,我可以能夠從我的功能回報可變
爲什麼'返回打印$ answer' - 爲什麼要打印?順便說一句:你應該找到一個更好的方法,而不是內部的foreach()。 – clemens321
我建議你先閱讀一些有關** [如何提出完美問題](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)**和** [如何創建一個最小化,完整和可驗證的示例](https://stackoverflow.com/help/mcve)**然後相應地編輯您的問題,以吸引更多人到您的文章。 –
你的json不好,它包含單引號 – miknik