【发布时间】:2021-04-18 05:15:59
【问题描述】:
如何将键“制造商”重命名为“test123”。
要求:
- 不将集合转换为数组。
- 不使用数据库查询“manufacturer as test123”。
收藏$stuff:
Illuminate\Pagination\LengthAwarePaginator {#543 ▼
#total: 14
#lastPage: 3
#items: Illuminate\Support\Collection {#541 ▼
#items: array:5 [▼
0 => {#447 ▼
+"id": 30
+"title": "some nice title"
+"properties": array:4 [▼
"price" => "99999"
"manufacturer" => "Puma"
"model" => "some model"
"condition" => "new"
]
}
1 => {#449 ▶}
2 => {#448 ▶}
3 => {#450 ▶}
4 => {#445 ▶}
]
}
#perPage: 5
#currentPage: 1
#path: "https://some-nice-url.com"
#query: []
#fragment: null
#pageName: "page"
+onEachSide: 3
#options: []
}
当我使用下面的代码时,我改变了属性的值而不是键。
foreach ($stuff as $key => $value) {
if(!empty($value->properties)) {
foreach ($value->properties as $key2 => $value2) {
if(array_key_exists($key2, $ok))
{
$value->properties[$key2] = $ok[$key2]['name'];
}
}
}
}
【问题讨论】: