Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:无法访问数组中的对象键_Php_Arrays_Object - Fatal编程技术网

PHP:无法访问数组中的对象键

PHP:无法访问数组中的对象键,php,arrays,object,Php,Arrays,Object,我有一个数组中的对象。我想访问这些对象的属性,但我没有任何运气 class Article { public $category; public $title; public $img; public $location; public $text; public function __construct($category, $title, $img, $location, $text) { $this->categor

我有一个数组中的对象。我想访问这些对象的属性,但我没有任何运气

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"];
类文章{
公帑类别;;
公有产权;
公帑$img;
公共场所;
公共文本;
公共函数构造($category、$title、$img、$location、$text){
$this->category=$category;
$this->title=$title;
$this->img=$img;
$this->location=“pages/”$location;
$this->text=$text;
}
}
//物品数据库
$runawayFive=新文章(“新闻”,“逃亡五人来到四方”,“img/BluesBrothers.jpg”,
“逃跑的五个人来到了fourside.html”,
“五号跑道继续其全国巡演,在四面停下来在世界著名的托波拉剧院演出。此前,一次意外的延误导致该团队将演出推迟了一周多。获奖团队被迫在邻近的小镇三维加速十天。往返三维的隧道被封锁,但没有人能阻止我知道怎么做或为什么。”。“
” “逃跑的五个人将在周五和周六晚上在托波拉剧院演出。门票为每位成人20美元。” ); $articles=数组($runawayFive); echo$articles[0][“title”];

我本该把文章的标题写出来的,但我什么也得不到。我可以执行
var\u dump($articles[0])
并返回对象,但无法访问其值。

在PHP中,您可以使用
->
操作符访问对象属性

echo $articles[0]->title;
试试这个

echo $articles[0]->title;

更多信息和示例可在此处找到

您可以像这样直接访问对象属性

echo $runawayFive->title;
不需要数组转换

$articles
它是一个数组,这个数组只有一个值,而数组中唯一的值是一个类型
文章
中的对象

array(
    0 => new Article()
);
可以通过键引用数组的每个值,默认情况下,键是从零开始的数字索引。因此,您可以通过以下方式访问阵列:

$articles[ $indexValue ];
在这种情况下,您可以使用以下内容:

$article = $articles[ 0 ];
访问索引0中数组的值。在这种情况下,这是一个对象。因此,要访问对象的非静态方法或实例变量,可以使用
->
操作符。具体如下:

$article->title;
短sintax是:

$articles[0]["title"];
还有一个更好的:

$article = $articles[0];
$article->title;
对于输出值,只需在调用实例变量之前写入
echo

比如:

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


是的,您必须访问如下属性:

$articles[0]->title;
或者,如果您确实希望以数组格式访问属性,可以实现如下所示的“ArrayAccess”:

class Article implements ArrayAccess {

 public $title;

}

$article = new Article();
echo $article['title'];
供参考:


看起来康德比你抢先一步。
class Article implements ArrayAccess {

 public $title;

}

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