我有一个从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)
得到了上述对象。
您可以看到“命中”有10个数组元素,所以我想说“对于每个“命中”结果,设置“ fieldname” = category_wrapper_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'];
}
}
但是它不起作用,我认为这与嵌套循环有关。
如何在这里只返回每个结果所需的值?