【问题标题】:Looping through array and pass data to function循环遍历数组并将数据传递给函数
【发布时间】:2016-10-02 07:57:02
【问题描述】:

我最近开始学习 Laravel,我正在尝试为一个项目实现 whmcs 的 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;
    }

【问题讨论】:

    标签: php arrays laravel


    【解决方案1】:

    你在你的 foreach() 循环中返回了一些东西。这意味着它将在第一个循环中停止。

    你可以这样做:

    $tickets = $this->getTickets($uid);
    
    $tickets_content = [];
    
    foreach ($tickets['ticket'] as $getid)
    {
    
        $id = $getid['id'];
    
        $tickets_content[] = $this->getTicket($id);
    
    }
    
    return $tickets_content;
    

    【讨论】:

      【解决方案2】:

      也许使用集合

      return collect($this->getTickets($uid))->map(function($ticket){
      
          return [$ticket, $this->getTicket($ticket['id'])];
      
      })->flatten(1);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-06
        • 1970-01-01
        • 2019-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-19
        • 2017-08-16
        • 1970-01-01
        相关资源
        最近更新 更多