简体   繁体   中英

Get array data from JSON

My target is to make an array of Objets(Article), I manage to recuperate the pmid, doi and title but when it comes to the array location , I couldn't do it, this the code I have tried:

<?php
class Article
{
    private $pmid;
    private $doi;
    private $title;
    private $location=array();

    public function setLocations($tabLocations){
        foreach ($tabLocations as $key => $value) {
            $location[]=$value;
        }
    }
    public function setPmid($_pmid){
        $this->pmid=$_pmid;
    }
    public function setDoi($_doi){
        $this->doi=$_doi;
    }
    public function setTitle($_title){
        $this->title=$_title;
    }

    //getters
    public function getTitle(){
        return $this->title;
    }
}

//get pmid from DOM
    require_once "simple_html_dom.php";
    $html=new simple_html_dom();
    $html->load_file('http://brainspell.org/search?query=psen+1');
    $pmid=array();

    foreach ($html->find('.paper-stuff') as $article) {
    $pmid[]= str_replace("/article/","",$article->find('a',0)->getAttribute('href'));
        }
//Create ALLArticles TAb
$articlesTab=array(); 

//upload data file
     $json=file_get_contents("C:\wamp\www\projet1\json\allArticles.json");
     $data=json_decode($json,true);         

    foreach ($pmid as $tabPmid) {
    foreach ($data as $key=>$value) {
        if($value["pmid"]==$tabPmid)
            {
                $details=new Article();

                $details->setPmid($value["pmid"]);
                $details->setDoi($value["doi"]);
                $details->setTitle($value["title"]);
                $details->setLocations($value['experiment']['location']);
                $articlesTab[]=$details;
            }           
     }         
 }
?>

This a portion of my JSON code:

{
   "papers": {
      "paper": [
         {
            "pmid": "6330179",
            "doi": "10.1002/cne.902260203",
            "experiment": [
               {
                  "id": "12789",
                  "location": [
                     "-46.0,-80.0,2.0",
                     "-42.0,-76.0,-2.0",
                     "44.0,-72.0,-2.0",
                     "52.0,-68.0,-6.0",
                     "-32.0,-88.0,4.0",
                     "-32.0,-90.0,8.0",
                     "34.0,-82.0,8.0",
                     "34.0,-78.0,8.0",
                     "-30.0,-52.0,-12.0",
                     "-30.0,-52.0,-10.0",
                     "28.0,-66.0,-16.0",
                     "26.0,-66.0,-12.0",
                     "-46.0,-76.0,6.0",
                     "-46.0,-78.0,0.0",
                     "46.0,-70.0,4.0",
                     "48.0,-74.0,0.0"
                  ]
               }
            ]
},
         {
            "pmid": "10022492",
            "doi": "10.1093/cercor/9.1.20",
            "experiment": [
               {
                  "id": "258",
                  "location": [
                     "-42.0,26.0,20.0",
                     "-36.0,30.0,16.0",
                     "-30.0,32.0,0.0",
                     "-28.0,14.0,48.0",
                     "-20.0,8.0,56.0",
                     "-32.0,4.0,52.0",
                     "50.0,10.0,36.0",
                     "48.0,24.0,20.0",
                     "32.0,0.0,56.0",
                     "8.0,-2.0,64.0",
                     "4.0,-8.0,68.0",
                     "-16.0,-4.0,12.0",
                     "-2.0,-18.0,12.0",
                     "-8.0,2.0,8.0",
                     "-44.0,-28.0,32.0",
                     "-4.0,-60.0,60.0",
                     "36.0,-58.0,52.0"
                  ]
               },

If there is anyone that could help me.Thanks in advance :D

If you notice location is another array so for you to get all its elements, it needs to be imploded. so try something like this .

$details->setLocations(implode(",",$value['experiment']['location']));

This will print the array as a string separated by comma ,; maybe you can choose what you want as the separator.

You simply missed $this to reference your object property in setLocations

public function setLocations($tabLocations){
    foreach ($tabLocations as $key => $value) {
        $this->location[]=$value; // $this->location is where you want to store your locations
    }
}

and by the way, I don't see any need to iterate over $tabLocations in your setter. Wouldn't

public function setLocations($tabLocations){
    $this->locations = $tabLocations;
}

do the trick too?

Or if you want to get rid of the keys:

public function setLocations($tabLocations){
    $this->locations = array_values($tabLocations);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM