我最近开始学习Laravel,我正在尝试为项目实现whm的api。我想循环一个数组,获取多个支持票证(对话)的id,并将这些id传递给调用我的后端来检索这些票证的函数。
以下函数返回用户拥有的所有票证。它包含票证ID(tid),并且具有以下功能,必须进行第二次API调用以检索票证的内容。
public function getTickets($userid)
{
$tickets = Whmcs::execute('gettickets',
array(
'clientid' => $userid,
)
);
return $tickets['tickets'];
}
$ tickets ['ticket']包含此数组:
{"ticket":
[{"
id":"2",
"tid":"363592",
"deptid":"1",
"userid":"1",
"name":"Ilyas Deckers",
"email":"deckersilyas@gmail.com",
"cc":"","c":"lOXHYuKV",
"date":"2016-06-02 13:50:16",
"subject":"Another ticket",
"status":"Open",
"priority":"Medium",
"admin":"","attachment":"","lastreply":"2016-06-02 13:50:16","flag":"0","service":""},
{ "id":"1",
"tid":"701236",
"deptid":"1",
"userid":"1",
"name":"Ilyas Deckers",
"email":"deckersilyas@gmail.com","cc":"","c":"76Rl9wKG",
"date":"2016-06-02 12:28:47","subject":"Theory of design","status":"Customer-Reply","priority":"Medium","admin":"","attachment":"","lastreply":"2016-06-02 12:30:03","flag":"0","service":""}]}
此阵列有2张票,id 1和id 2.如果我想知道这些票的内容是什么,我可以使用以下内容获取该数据:
function getTicket($id)
{
$ticket = Whmcs::execute('getticket',
array(
'ticketid' => $id,
)
);
return $ticket;
}
当我传递一个id时返回此数组:
{
"result":"success",
"ticketid":"1",
"tid":"701236",
"name":"Ilyas Deckers",
...
"priority":"Medium",
"admin":"",
"lastreply":"2016-06-02 12:30:03",
"flag":"0","service":"",
"replies":{
"reply":[{
"userid":"1",
"contactid":"0",
"name":"Ilyas Deckers",
"email":"deckersilyas@gmail.com",
"date":"2016-06-02 12:28:47",
"message":"Hi Steve,\r\n\r\nVisual hierarchy is one of the most important principles behind effective web design. We will see how you can use some very basic exercises in your own designs to put these principles into practice.\r\n\r\nDesign is a funny word. Some people think design means how it looks. But of course, if you dig deeper, it's really how it works.\r\nAt it's core, design is all about visual communication: To be an effective designer, you have to be able to clearly communicate your ideas to viewers or else lose their attention.\n\n----------------------------\nIP Address: 127.0.0.1",
"attachment":"",
"admin":""},
{
"userid":"1",
"contactid":"0",
"name":"Ilyas Deckers",
"email":"deckersilyas@gmail.com",
"date":"2016-06-02 12:30:03",
"message":"Hi Steve,\r\n\r\nVisual hierarchy is one of the most important principles behind effective web design. We will see how you can use some very basic exercises in your own designs to put these principles into practice.\r\n\r\nDesign is a funny word. Some people think design means how it looks. But of course, if you dig deeper, it's really how it works.\r\nAt it's core, design is all about visual communication: To be an effective designer, you have to be able to clearly communicate your ideas to viewers or else lose their attention.",
"attachment":"863372_Clientv2.PNG",
"admin":"",
"rating":"0"
}]},
"notes":""}
如何通过相同的函数传递$ id = 1和$ id = 2,并将这些结果合并到一个数组中,以便在模板中的foreach循环中使用?我想通过第一个数组循环,获取id并通过函数getTicket($ id)传递它。这有效,但只能给出一个结果。
$tickets = $this->getTickets($uid);
foreach ($tickets['ticket'] as $getid)
{
$id = $getid['id'];
$this->getTicket($id);
return $ticket;
}
答案 0 :(得分:3)
你在foreach()循环中返回一些东西。这意味着它将在第一个循环中停止。
您可以执行以下操作:
$tickets = $this->getTickets($uid);
$tickets_content = [];
foreach ($tickets['ticket'] as $getid)
{
$id = $getid['id'];
$tickets_content[] = $this->getTicket($id);
}
return $tickets_content;
答案 1 :(得分:0)
也许正在使用馆藏
return collect($this->getTickets($uid))->map(function($ticket){
return [$ticket, $this->getTicket($ticket['id'])];
})->flatten(1);