所以,我有一个想要“转换”的数组(只添加该数组的一些值,到具有不同键的不同数组)。
最好的方法是什么?我目前有一种方法可以做到这一点,但我认为有更好的方法来做到这一点。
例如:一个 API 返回以下内容:
[
'id' => 1,
'title' => 'Response title',
'message' => 'Here is a message',
'clueless' => true,
'need_help' => true
]
但是对于我的前端,我需要这个:
[
'task_id' => 1,
'text' => 'Response title',
'message' => 'Here is a message'
]
请注意,这些值是相同的,但只有它们的键不同。另外,有些值我不需要。
我目前的方法如下所示:
public function toSpecificFormat($data) {
return [
'task_id' => $data['id'],
'text' => $data['title'],
'message' => $data['message']
];
}
桃花长相依