duanjiong5023 2017-06-23 10:24
浏览 248
已采纳

如何解析并保存到数据库的json数组?

I have a json file structured like this :

 [{"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."]
}]

When i json_decode() the json gives me this result :

 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. ) ) ) 

My difficulty is when i want to save all [Molecules][Main name] it saves only one [Main name]. I'd like to save it all as string, so i tried this :

 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);
    }
}

I've got an error Warning: implode(): Argument must be an array while it works on [Synonyms].

So my question is : how to save to my database all [Main names] ?

I tried many others ways with no results so i come here and hope someone know how to do that. Best regards.

  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 关于#glmb#的问题,如何解决?
      • ¥15 TI的TMS320F28335,RS485串口进行SCI-B通信出错
      • ¥15 XML文件报错不允许出现此特性,百度也搜不到解决方法
      • ¥15 FTPC共同交流学习
      • ¥15 关于#python##pyqt#的问题,如何解决?
      • ¥15 html+css网页设计 html+css网页设计
      • ¥15 用html加css制作一个网页
      • ¥200 MFC中如何对ListCtrl的某一列的文本能换行显示
      • ¥15 clion添加库文件
      • ¥15 ERROR C2143怎么出现的?为什么ERROR C2144会这么转移,它是怎么出现的?该怎么解决?