【发布时间】:2013-05-09 02:07:42
【问题描述】:
更新:我收到此 php 错误:尝试在此行上获取非对象的属性: if ($card->name == $fileRef){
我正在使用__construct(x='') { definition } 在php 中构造一个对象并调用函数$var = new Object('string');
构造函数接收一个字符串,即与相应的“file.php”相关的“file”。在一个json文件中有这个类的所有可用file.php的目录,其中包含目录信息。
我发现了一个错误,也许是我的语法? ...现在有两天了,想试试吗?
public function __construct($ctitle = '')
{
$fileRef = $ctitle.'php';
//Get the json card directory
$this->cardDirLocation = 'thisismyserver.com/CardDir.json';
$this->cardDir = file_get_contents($this->cardDirLocation);
$this->cardArray = json_decode($this->cardDir, true);
//Find the card listing from CardDir.json based on form response input and construct a Card class instance or use the main page (default)
foreach ($this->cardArray['Cards'] as $key => $val) { //search through each card IS THIS MY ERR??
if ($card->name == $fileRef){ //if the name matches a name in the cardDir.json file
//Fill Values
}
if ($this->title == ''){ //or if there is no title
$this->title = 'Get Started at The Home Page'; //refer to default values -> the home page
$this->dir = 'cards/start/';
$this->name = 'start.php'; ...etc
错误:我无法让 $fileRef 变量与 json 文件数组中的任何内容相匹配,因此它总是进入“else if”默认值。 json 文件如下所示:
{"卡片":[{"title":"某事", “名称”:“文件.php”, “目录”:“某个文件/目录/这里/” }, {"title":"不同于某物", "名称":"xfiles.php", "dir":"somefile/dir/there/" ...等
【问题讨论】:
-
echo: $val['name']这行输出什么? -
谢谢 已回答。
标签: php json object foreach multidimensional-array