首页 > 解决方案 > 在嵌套数组 php 转储中显示第二个数组的结果

问题描述

我有以下代码:

public function canceledReasons()
{
   $searchs = Zendesk::search()->find('type:ticket tags:cancel');
   $searchs = json_decode(json_encode($searchs),true);
   foreach ($searchs as $search) {
        dump($search);
   }
}

它返回一个数组,其中包含一些嵌套数组我试图在“标签”中显示第二个数组(如果存在)。那里可能有多个数组,因此最好将它们全部显示出来。
Results

array:100 [▼
  0 => array:36 [▼
    "url" => "https://example/api/v2/tickets/example.json"
    "id" => example
    "external_id" => null
    "via" => array:2 [ …2]
    "created_at" => "2019-08-28T02:05:22Z"
    "updated_at" => "2019-08-29T23:00:54Z"
    "type" => null
    "subject" => "example"
    "raw_subject" => "example"
    "description" => """
      From: example\n
      Phone: \n
      Location: example\n
      URL: https://example.zendesk.com/hc/en-us/categories/#####-Account-Billing\n
      Department: \n
      \n
      Hi,\n
      please cancel  ▶
      \n
      Best,\n
      Vinay\n
      \n
      ----\n
      Zopim\n
      https://www.example.com
      """
    "priority" => null
    "status" => "open"
    "recipient" => "example"
    "requester_id" => example
    "submitter_id" => example
    "assignee_id" => example
    "organization_id" => null
    "group_id" => example
    "collaborator_ids" => []
    "follower_ids" => []
    "email_cc_ids" => []
    "forum_topic_id" => null
    "problem_id" => null
    "has_incidents" => false
    "is_public" => true
    "due_at" => null
    "tags" => array:2 [ …2]
    "custom_fields" => array:2 [ …2]
    "satisfaction_rating" => null
    "sharing_agreement_ids" => []
    "fields" => array:2 [ …2]
    "followup_ids" => []
    "brand_id" => example
    "allow_channelback" => false
    "allow_attachments" => true
    "result_type" => "ticket"
  ]
  1 => array:36 [▶]
  2 => array:36 [▶]
  3 => array:36 [▶]
  4 => array:36 [▶]

当我尝试

dump($search['tags']);

它返回未定义的索引:标签

只是试图获得“标签”数组的结果。

谢谢您的帮助。

标签: phparrayslaravel

解决方案


尝试这个...

public function canceledReasons()
{

  $searchs = Zendesk::search()->find('type:ticket tags:cancel');
  $searchs = json_decode(json_encode($searchs),true);

  foreach ($searchs as $search) {
    dump($search[0]['tags']);
     //or to get all tags array value
    foreach ($search[0]['tags'] as $tag) {
       dump($tag);
    }
  }
}
    ```

推荐阅读