在laravel控制器中创建数据数组

时间:2018-03-21 05:39:45

标签: arrays laravel arraylist

OkHttpClient client = new OkHttpClient.Builder()
            // .addInterceptor(interceptor)
            .addNetworkInterceptor(interceptor)

当我尝试使用$document = new Document(); $document->title = $request['title']; $document->description = $request['description']; 输出上述代码时,我得到了这个结果:

echo $document;

我想要的是创建自己的数据数组并输出相同的格式。这是我试图尝试的代码,但它不起作用:

{"title":"asdfasdf","description":"asdfasdfsadf"}

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:2)

所有集合也充当迭代器,允许您循环遍历它们,就像它们是简单的PHP数组一样:

foreach ($document as $data) {
    echo $data->title;
    echo $data->description;
}

使用PHP框架时没有区别。您可以参考官方PHP Arrays Manual页面来使用语言结构。

如果您需要将JSON转换为数组,请使用:

  

<强> $ DATA-&GT;指定者();

OR

  

<强> json_decode($数据);

这是你的代码:

$data = array(
  "title" => "hello",
  "description" => "test test test"
);

//也可以声明

$data = ["title" => "hello", "description" => "test test test"];

使用:

  

<强>的var_dump($数据);

OR

  

<强>的print_r($数据);

//输出将是

["title" => "hello", "description" => "test test test",]