Probleam使用类、数组、json和php

Probleam使用类、数组、json和php,php,arrays,json,Php,Arrays,Json,您好,这是我第一次尝试用PHP创建一些代码,花了我很长时间,但我能够将数据转换为xml。现在我需要创建一个JSON对象,但它运行得不太顺利。最大的问题是试图在PHP中创建一个新类(我不知道我所做的是否正确),以及这是否是附加列表的正确方法。我认为其中一些是好的,但对我来说,因为我只使用java和c,这似乎有点疯狂。我想我做错了什么。显示错误的行是$array['data']->attach(new-Cake($name,$components,$prepare,$image))

您好,这是我第一次尝试用PHP创建一些代码,花了我很长时间,但我能够将数据转换为xml。现在我需要创建一个JSON对象,但它运行得不太顺利。最大的问题是试图在PHP中创建一个新类(我不知道我所做的是否正确),以及这是否是附加列表的正确方法。我认为其中一些是好的,但对我来说,因为我只使用java和c,这似乎有点疯狂。我想我做错了什么。显示错误的行是
$array['data']->attach(new-Cake($name,$components,$prepare,$image))
谢谢

//opens the file, if it doesn't exist, it creates
$pointer = fopen($file, "w");

// writes into json


$cake_list['data'] = new SplObjectStorage();
for ($i = 0; $i < $row; $i++) {

    // Takes the SQL data
    $name = mysql_result($sql, $i, "B.nome");
    $ingredients = mysql_result($sql, $i, "B.ingredientes");
    $prepare = mysql_result($sql, $i, "B.preparo");
    $image = mysql_result($sql, $i, "B.imagem");

    // assembles the xml tags
    // $content = "{";

    // $content .= "}";

    $array['data']->attach( new Cake($name,$ingredients,$prepare,$image));
    // $content .= ",";
    // Writes in file
    // echo $content;
    $content = json_encode($content);

    fwrite($pointer, $content);
    // echo $content;
} // close FOR

echo cake_list;

// close the file
fclose($pointer);

// message
// echo "The file <b> ".$file."</b> was created successfully !";
// closes IF($row)

class Cake {
    var $name;
    var $ingredients;
    var $prepare;
    var $image;

    public function __construct($name, $ingredients, $prepare, $image)
    {
        $this->name = $name;
        $this->ingredients = $ingredients;
    $this->prepare = $prepare;
    $this->image = $image;
    }
}

function create_instance($class, $arg1, $arg2, $arg3, $arg4)
{
    $reflection_class = new ReflectionClass($class);
    return $reflection_class->newInstanceArgs($arg1, $arg2,$arg3, $arg4);
}
//打开文件,如果文件不存在,则创建
$pointer=fopen($file,“w”);
//写入json
$cake_list['data']=新的SplObjectStorage();
对于($i=0;$i<$row;$i++){
//获取SQL数据
$name=mysql_结果($sql,$i,“B.nome”);
$components=mysql_结果($sql,$i,“B.ingredientes”);
$prepare=mysql_result($sql,$i,“B.preparo”);
$image=mysql_结果($sql,$i,“B.imagem”);
//组装xml标记
//$content=“{”;
//$content.=“}”;
$array['data']->附加(新蛋糕($name、$factors、$prepare、$image));
//$content.=“,”;
//写入文件
//echo$内容;
$content=json_encode($content);
fwrite($pointer,$content);
//echo$内容;
}//关闭
回音蛋糕列表;
//关闭文件
fclose($指针);
//信息
//echo“文件”.$file.“已成功创建!”;
//如果($row)则关闭
班级蛋糕{
var$name;
var$成分;
var$prepare;
var$图像;
公共函数构造($name、$Components、$prepare、$image)
{
$this->name=$name;
$this->components=$components;
$this->prepare=$prepare;
$this->image=$image;
}
}
函数create_实例($class、$arg1、$arg2、$arg3、$arg4)
{
$reflection\u class=新的ReflectionClass($class);
返回$reflection\u class->newInstanceArgs($arg1、$arg2、$arg3、$arg4);
}

您遇到的错误是因为您在执行
$array['data']
而您的意思是
$cake\u list['data']
所以将错误行更改为:

$cake_list['data']->attach(new Cake($name, $ingredients, $prepare, $image));
创建JSON对象(或者更准确地说是JSON对象的字符串表示)的简单方法是:

$array = array(
    'name' => $name,
    'ingredients' => $ingredients,
    'prepare' => $prepare,
    'image' => $image
);

$json = json_encode($array);
您还可以创建简单易用的对象,如下所示:

$myObject = new stdClass(); // stdClass() is generic class in PHP

$myObject->name        = $name;
$myObject->ingredients = $ingredients;
$myObject->prepare     = $prepare;
$myObject->image       = $image;
除非存在特定问题,否则是否可以将其迁移到?