无法在php中使用foreach正确保存数据

无法在php中使用foreach正确保存数据,php,laravel,multidimensional-array,dynamic-arrays,Php,Laravel,Multidimensional Array,Dynamic Arrays,我将$data作为参数传递给模型中的setter函数变量转储($data)给出 array(2) { ["iconUrl"]=> string(31) "C:\fakepath\photo-125301449.jpg" ["title"]=> string(8) "Example1" } array(2) { ["iconUrl"]=> string(30) "C:\fakepath\photo-71823413.jpg" ["title"]=>

我将
$data
作为参数传递给模型中的setter函数<代码>变量转储($data)给出

array(2) {
  ["iconUrl"]=>
  string(31) "C:\fakepath\photo-125301449.jpg"
  ["title"]=>
  string(8) "Example1"
}
array(2) {
  ["iconUrl"]=>
  string(30) "C:\fakepath\photo-71823413.jpg"
  ["title"]=>
  string(12) "Example2"
}
然后我在setter函数中执行一个
foreach

foreach($data as $val) {
    $this->goalLink = [
        'iconUrl' => $val['iconUrl'],
        'title' => $val['title']
    ];
}
构造函数的定义如下

protected $goalLink = [];
public function __construct() {
    $this->goalLink = [
        [
            'iconUrl' => null,
            'title' => null
        ]
    ];   
}
goaloalLink":[{"iconUrl":"C:\fakepath\photo-125301449.jpg","title":"Example1"}, {"iconUrl":"C:\\fakepath\\photo-71823413.jpg","title":"Example2"}]
最后还有一个
createNew()
,它将数据发送到我的API

$client = API::client();
$url = API::url('exampleURL');
$data = [
    'goalLink' => [
        'iconUrl' => $this->goalLink['iconUrl'],
        'title' => $this->goalLink['title']
    ]
];
$response = $client->post($url, ['json' => $data]);
现在的问题是数据保存不正确,只有来自第二个阵列的数据被保存。数据在某处被覆盖

这是我从API得到的响应

goalLink":[{"iconUrl":"C:\\fakepath\\photo-71823413.jpg","title":"Example2"}]
但事实上,它应该像这样保存

protected $goalLink = [];
public function __construct() {
    $this->goalLink = [
        [
            'iconUrl' => null,
            'title' => null
        ]
    ];   
}
goaloalLink":[{"iconUrl":"C:\fakepath\photo-125301449.jpg","title":"Example1"}, {"iconUrl":"C:\\fakepath\\photo-71823413.jpg","title":"Example2"}]

您正在覆盖
$this->goalLink
的完整值,而不是附加到数组中。那么,将数据附加到数组中的正确方法是什么?请参阅。换句话说,
$this->goalLink[]=$something
;你好像没有看到方括号。