我有一个像这样结构的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 :(得分: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);
}
}
主名不是数组,而是字符串。如果你想破坏它,你必须先把它变成一个数组。