2017-06-23 105 views
1

我有結構化的這樣一個JSON文件:如何解析並保存到數據庫的JSON數組?

[{"id":"PMC4640113", 
"Original_paper":"https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4640113/pdf", 
"Title":"Esculin improves dyslipidemia, inflammation and renal damage in streptozotocin-induced diabetic rats", 
"Molecules":[{"Main name":"triglyceride", "Synonyms":[]},{"Main name":"acid", "Synonyms":[]},{"Main name":"lipids", "Synonyms":[]},{"Main name":"coumarin", "Synonyms":[]},{"Main name":"coumarins", "Synonyms":[]},{"Main name":"esculetin", "Synonyms":["esculin"]},{"Main name":"fraxetin",   "Synonyms":[]},{"Main name":"triglycerides", "Synonyms":[]},{"Main name":"sugar", "Synonyms":[]}], 
"ToxKeywords":"nephrotoxicity, ", 
"Important_sentences":["Naaz F, et al. [20] demonstrated that the protective efficacy of esculin against pro-oxidant AFB1-induced nephrotoxicity in mice might be due to its antioxidants and free radical scavenging properties."] 
}] 

當我json_decode()的JSON給了我這樣的結果:

Array ([0] => Array ( 
[id] => PMC4640113 
[Original_paper] => https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4640113/pdf 
[Title] => Esculin improves dyslipidemia, inflammation and renal damage in streptozotocin-induced diabetic rats 
[Molecules] => Array ([0] => Array ([Main name] => triglyceride [Synonyms] => Array ()) [1] => Array ([Main name] => acid [Synonyms] => Array ()) [2] => Array ([Main name] => lipids [Synonyms] => Array ()) [3] => Array ([Main name] => coumarin [Synonyms] => Array ()) [4] => Array ([Main name] => coumarins [Synonyms] => Array ()) [5] => Array ([Main name] => esculetin [Synonyms] => Array ([0] => esculin)) [6] => Array ([Main name] => fraxetin [Synonyms] => Array ()) [7] => Array ([Main name] => triglycerides [Synonyms] => Array ()) [8] => Array ([Main name] => sugar [Synonyms] => Array ())) 
[ToxKeywords] => nephrotoxicity, 
[Important_sentences] => Array ([0] => Naaz F, et al. [20] demonstrated that the protective efficacy of esculin against pro-oxidant AFB1-induced nephrotoxicity in mice might be due to its antioxidants and free radical scavenging properties.))) 

我的困難是,當我要保存所有[Molecules][Main name]它僅保存一個[主要名稱]。我想這一切保存爲字符串,所以我想這:

if (is_array($array)) { 
    foreach ($array as $item) { 
     $jsonTextMining = new JsonTextMining(); 
     $jsonTextMining->setSolrId($item['id']); 
     $jsonTextMining->setOriginalPaper($item['Original_paper']); 
     $jsonTextMining->setTitle($item['Title']); 
     foreach ($item['Molecules'] as $mol) { 
      $jsonTextMining->setMoleculeName(implode($mol['Main name'])); 
     } 
     foreach ($item['Molecules'] as $mol) { 
      $jsonTextMining->setSynonymName(implode($mol['Synonyms'])); 
     } 
     $jsonTextMining->setKeyword($item['ToxKeywords']); 
     $jsonTextMining->setImportantSentence(implode($item['Important_sentences'])); 

     $em = $this->getDoctrine()->getManager(); 
     $em->persist($jsonTextMining); 
    } 
} 

我有一個錯誤Warning: implode(): Argument must be an array,而它的工作原理上[別名]。

所以我的問題是:如何保存到我的數據庫全部[Main names]

我嘗試了很多其他的方式沒有結果,所以我來到這裏,希望有人知道如何做到這一點。 此致敬禮。

回答

0
if (is_array($array)) { 
    foreach ($array as $item) { 
     $jsonTextMining = new JsonTextMining(); 
     $jsonTextMining->setSolrId($item['id']); 
     $jsonTextMining->setOriginalPaper($item['Original_paper']); 
     $jsonTextMining->setTitle($item['Title']); 
     $mol_mainname = array(); 
     foreach ($item['Molecules'] as $mol) { 
      array_push($mol_mainname,$mol['Main name']); 
     } 
     $jsonTextMining->setMoleculeName(implode($mol_mainname)); 
     foreach ($item['Molecules'] as $mol) { 
      $jsonTextMining->setSynonymName(implode($mol['Synonyms'])); 
     } 
     $jsonTextMining->setKeyword($item['ToxKeywords']); 
     $jsonTextMining->setImportantSentence(implode($item['Important_sentences'])); 

     $em = $this->getDoctrine()->getManager(); 
     $em->persist($jsonTextMining); 
    } 
} 

主要名稱不是數組,它是一個字符串。如果你想破壞它,你必須先把它放到一個數組中。

+0

謝謝你的回答,這幾乎是它,但它給了我沒有空間之間的名字是否有辦法在它們之間放置空間或caractere? – Gy0m

+0

'implode(「」,$ mol_mainname)' – selten98

+0

非常感謝你 – Gy0m