2016-10-11 1 views
0

Mein Ziel ist es, ein Array von Objets (Artikel), ich es schaffen, die pmid, doi and title zu erholen, aber wenn es um das Array location kommt, konnte ich es nicht tun, das ist der Code, den ich ausprobiert habe:Get Array-Daten von JSON

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

Dies ist ein Teil meines 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" 
        ] 
       }, 

Wenn es jemanden gibt, der me.Thanks im Voraus helfen könnte: D

Antwort

0

Wenn Sie feststellen, dass der Standort ein anderes Array ist, müssen Sie implodieren, damit Sie alle Elemente erhalten. Also versuche so etwas.

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

Dies wird durch ein Komma, das Array als eine Zeichenfolge getrennt drucken; vielleicht können Sie wählen, was Sie als Trennzeichen wollen.

+0

So werden Sie nicht ein Array als Argument der setLocations() in der Lage escept zu setzen, wenn Sie es vorgegeben, ein Array zu nehmen. – Po10cio

0

Sie verfehlten einfach $this Ihre Objekteigenschaft in setLocations

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

und durch die Art und Weise zu verweisen, ich sehe keine Notwendigkeit, über $ tabLocations in Ihrem Setter iterieren. Würde nicht

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

den Trick auch tun?

Oder wenn Sie der Schlüssel loswerden wollen:

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