2013-05-07 18 views
6

ho il seguente output:Accesso oggetto all'interno Array

Array (
    [0] => stdClass Object (
     [id] => 20 
     [news_title] => Startup finance docs in GitHub 
     [news_url] => http://venturebeat.com/2013/03/06/fenwick-west-github/ 
     [news_root_domain] => venturebeat.com 
     [news_category] => 
     [news_submitter] => 4 
     [news_time] => 2013-03-06 11:20:03 
     [news_points] => 0 
    ) 
    [1] => stdClass Object (
     [id] => 21 
     [news_title] => The problems with righteous investing 
     [news_url] => http://gigaom.com/2013/03/07/the-problems-with-righteous-investing/ 
     [news_root_domain] => gigaom.com 
     [news_category] => 
     [news_submitter] => 4 
     [news_time] => 2013-03-08 09:14:17 
     [news_points] => 0 
    ) 
) 

Come potrei accedere a qualcosa come news_url a questi? Ho provato questo, ma senza alcun risultato:

print_r $this->$record[0]->news_title; 
+1

probabilmente si vuole: 'print_r $ this-> RECORD [0] -> news_title;', nota ho rimosso il '$' in '$ record [0]'. – nickb

risposta

6

provo questo:

$arr = Array(); 

    $obj0 = new stdClass; 
    $obj0->id = 123; 
    $obj0->news_title = "some title 0"; 
    //etc... 
    $obj1 = new stdClass; 
    $obj1->id = 124; 
    $obj1->news_title = "some title 1"; 
    //etc... 

    $arr[0] = $obj0; 
    $arr[1] = $obj1; 

    print_r($arr); 

o qualcosa di simile

print_r($arr[0]); 

o anche

echo $arr[0]->id; 
+1

Saluti che l'ultimo ha funzionato. – viablepath

1

Si utilizza proprietà di classe, potresti voler controllare se è accessibile fi prima. Durante l'accesso alla proprietà della classe dopo aver utilizzato $this non è necessario l'ulteriore $, utilizzare solo $this - record. Come

echo $this -> record[0] -> title; 

Se record è una proprietà di classe valida, che è un array e ancora non funziona. Dare una prova troppo:

echo {$this -> record[0]} -> title;