php - 将数组转换为对象并打印结果
问题描述
我有这个对象和数组数据:
stdClass Object
(
[meta_title] => title
[meta_description] =>
[categories] => Array
(
[0] => stdClass Object
(
[name] => cat title
)
[1] => stdClass Object
(
[name] =>cat title 2
)
)
)
在行动中,我需要categories name
使用foreach
方法打印。我怎么打印这个?!
编辑:
在我的控制器中,我有:
public function index()
{
helper('text');
$data = array(
'meta_title' => 'title',
'meta_description' => '',
'categories' => $this->postModel->getNewsCategories(),
);
return view('blog/index', array( 'categories' => (object)$data));
}
鉴于:
<?php foreach ($categories as $category) : ?>
<a href="#"><?php echo esc($category->name);?></a>
<?end foreach;?>
但我看到这个错误:
Trying to get property 'name' of non-object
解决方案
我认为下面的代码会起作用
<?php foreach ($categories->categories as $category) } ?>
<a href="#"><?php echo esc($category->name);?></a>
<?php end foeach; } ?>
推荐阅读
- python - Python - 根据索引将元素(行和索引)移动到末尾
- sql-server - 有没有办法编写一个 sql 脚本,将一个表从一个数据库复制到另一个而不丢失 PK 和 FK?
- javascript - 使用 Jwt NodeJs Api 登录 .Net 身份用户
- php - 覆盖持有准备好的语句的变量会关闭该语句吗?
- r - 根据涉及另一列的条件在 r 中创建一列
- amazon-redshift - Glue Python Shell - 私有子网访问
- r - 条形图颜色布尔值
- php - 在连接表上获取错误数据
- spring-cloud-dataflow - 如何在一个 jar 中创建多个任务(Spring Cloud Task)?
- php - PHP解析ELA温度传感器原始数据