Php 从端点查询在对象上循环

Php 从端点查询在对象上循环,php,arrays,api,endpoint,Php,Arrays,Api,Endpoint,我有一个函数从API搜索端点返回以下内容: {#1090 ▼ +"took": 3 +"timed_out": false +"_shards": {#1141 ▶} +"hits": {#1309 ▼ +"total": 231 +"max_score": null +"hits": array:10 [▼ 0 => {#1191 ▼ +"_index": "products" +"_type": "product" +"_id":

我有一个函数从API搜索端点返回以下内容:

{#1090 ▼
+"took": 3
+"timed_out": false
+"_shards": {#1141 ▶}
+"hits": {#1309 ▼
  +"total": 231
  +"max_score": null
  +"hits": array:10 [▼
    0 => {#1191 ▼
      +"_index": "products"
      +"_type": "product"
      +"_id": "1634"
      +"_score": 5.7772136
      +"_source": {#1214 ▼
        +"category_wrapper_detail": "General 1234:0 - Dark Green 123:25"
        +"wrappers": {#1344 ▼
          +"1": {#1298 ▼
            +"wrapper_code": "123"
            +"id": 525
            +"detail_name": "Dark Green"
            +"detail_code": "25"
          }
        }
        +"template": {#1216 ▼
          +"category": {#1138 ▼
            +"category_code": "1234"
            +"category_name": "General"
            +"category_subcat": 0
            +"id": 135
          }
        }
      }
      +"fields": {#1116 ▶}
    }
    1 => {#1114 ▶}
    2 => {#1083 ▶}
    3 => {#1021 ▶}
    4 => {#950 ▶}
    5 => {#929 ▶}
    6 => {#764 ▶}
    7 => {#770 ▶}
    8 => {#703 ▶}
    9 => {#1535 ▶}
  ]
}
}

结果是正确的,但我的问题是我不知道如何正确地循环。我通过执行
dd($searchResults)
获得上述对象

您可以看到“hits”有10个数组元素,所以我想说“对于每个“hits”结果,设置“fieldname”=category\u wrapper\u detail,然后将彼此的值设置为它自己的变量”

我正在尝试这个:

foreach($searchResults as $result['hits'] => $result){
  foreach($result['source'] as $source){
      //This is the full concatenated result for a label
      $fullResult = ['category_wrapper_detail'];

      //The following are the individual results that I'll save on the backend eventually
      $categoryName = ['category_name'];
      $categoryCode = ['category_code'];
      $categorySubcat = ['category_subcat'];
      $detailName = ['detailName'];
      $wrapperCode = ['wrapper_code'];
      $detailCode = ['detail_code'];
  }
}
但它不起作用,我认为这与嵌套循环有关


我如何才能在这里只返回每个结果所需的值?

foreach($searchResults['hits']as$hit=>$result)
最好是从elasticsearch开始。您是否从elasticsearch获取数据?@smart-googler是的,这是我们网站中的elasticsearch集成API@Whisou138您是否可以编辑您的代码片段并发布数组
print';打印($searchResult['hits']['hits']);打印“”而不是api响应?我把它放进去了,但是我得到“不能使用stdClass作为数组?”?