gpt4 book ai didi

php - 从数组输出单个 json 对象

转载 作者:行者123 更新时间:2023-11-29 11:08:13 24 4
gpt4 key购买 nike

在 laravel 中,我试图将 json 数据传递到我的 profile.blade.php View 中。我的 apicontroller.php 中有 json 查询,它从我的配置文件数据库表中提取数据,然后使用 profilecontroller.js 将数据传递到 profile.blade.php 中。

我可以使用 @{{profiles }} 在 profile.blade.php 中将数据视为数组,我得到输出

[{"id":1,"用户名":"test","电子邮件":"test@gmail.com","密码":"$2y$10$q2N/p3AD8FfUHxRVOF.aoecIKy8qcOBgvVurP4tZrcRdvZSS3JDOK", "avatar":"default.jpg","re​​member_token":null,"created_at":"2016-12-07 22:29:57","updated_at":"2016-12-07 22:29:57"} ]

但是如果我尝试获取单个对象,例如 {{profiles.username }} 它不会输出任何内容

如何从数组中仅提取用户名或其他对象?

apiController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\UserDetail;
use App\Users;
use View;
use Auth;

class ApiController extends Controller
{
public function showAll()
{
return response()->json(
$details = Users::get()
);

}
}

profilecontroller.js

app.controller('profileController', function($scope, $http) {

$http.get('/api/hello')
.success(function(data, status, headers, config) {
$scope.profiles = data;
})
.error(function(error, status, headers, config) {
console.log(status);
console.log("Error occured");
});

profile.blade.php

@ {{profile}}

最佳答案

您的对象位于数组中,因此您必须指示代码引用的是哪个数组条目,即使数组中只有一个条目。

数组的索引为零,因此数组中的第一项始终具有索引 [0]

因此

@{{ profiles[0].username }}

应该实现你想要做的事情。

关于php - 从数组输出单个 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41036491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com