duanjiong5023
duanjiong5023
2017-06-23 10:24
浏览 187

如何解析并保存到数据库的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条回答 默认 最新

  • douzhi9635
    douzhi9635 2017-06-23 10:32
    已采纳
    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);
        }
    }
    

    Main name is not an array, it's a string. You must first make it into an array if you want to implode it.

    点赞 评论

相关推荐