2

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 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, for your answer, it's almost it but it gives me the names without space between them is there a way to put space or caractere between them ?
implode(" ", $mol_mainname)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.