简体   繁体   中英

PHP: Cannot access object key in array

I have objects in an Array. I want to access those objects' properties, but I'm not having any luck.

class Article {
    public $category;
    public $title;
    public $img;
    public $location;
    public $text;


    public function __construct($category, $title, $img, $location, $text) {
        $this->category = $category;
        $this->title = $title;
        $this->img = $img;
        $this->location = "pages/" . $location;
        $this->text = $text;
    }
}

//db of articles
$runawayFive = new Article("news", "The Runaway Five Comes to Fourside",     "img/BluesBrothers.jpg",
 "runaway_five_comes_to_fourside.html",
 "The Runway Five continues its nationwide tour, stopping in Fourside to    perform at the world famous Topolla Theater. Previously, an unexpected delay caused the group to postpone its shows for over a week. The award-winning group was forced to speed ten days in neighboring town, Threed. Tunnels going to and from Threed were blocked, but no one really knows how or why." . "<br>"
 ."The Runaway Five will being playing at the Topolla Theater during Friday and Saturday night. Tickets are $20 per adult."
);
$articles = Array($runawayFive);
echo $articles[0]["title"];

I should have the title of the article echoed out, but I'm not getting anything. I can do var_dump($articles[0]) and return the object, but can't access its values.

在PHP中,您可以使用->运算符访问对象属性。

echo $articles[0]->title;

Try this

echo $articles[0]->title;

More info and examples could be found here http://php.net/manual/en/language.types.object.php

You can access the object property directly like this

echo $runawayFive->title;

No need for an array conversion

$articles

It's an array, this array has only 1 value, and the only value you have in the arrray it's an object from type Article .

array(
    0 => new Article()
);

You can reference to each value of an array by the key, the key by default it's numeric index starting from zero. So you can access to the array by

$articles[ $indexValue ];

In this case you can have something like:

$article = $articles[ 0 ];

To access the value of the array in the index zero. So in this case this is an object. So to access to the non static methods or instance varialbes of an object you use the -> operator. Like follows:

$article->title;

The short sintax is:

$articles[0]["title"];

And a nicer one:

$article = $articles[0];
$article->title;

For output the value just write echo before the call to the instance variable.

Like:

 $article = $articles[0];
 echo $article->title;

OR

 echo $articles[0]->title;

Yep, you have to access the properties like this:

$articles[0]->title;

Or if you really want to access the properties in array format you can implement 'ArrayAccess' like below:

class Article implements ArrayAccess {

 public $title;

}

$article = new Article();
echo $article['title'];

For reference:

http://php.net/manual/en/class.arrayaccess.php http://php.net/manual/en/language.oop5.interfaces.php

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