[英]ErrorException Array to string conversion Laravel 8
I have JSON-formed data and it is converted into the array.我有 JSON 格式的数据,它被转换成数组。 But I cannot iterate the array and fetch the data, it shows an error.
但是我无法迭代数组并获取数据,它显示错误。
ErrorException Array to string conversion
ErrorException 数组到字符串的转换
Data:数据:
Array
(
[posts] => Array
(
[0] => Array
(
[post_id] => 5083755625086176
[text] => Carry on the Trend!
[links] => Array
(
[0] => Array
(
[link] => https:// .........
[text] => xyz
)
[1] => Array
(
[link] => https:// .........
[text] => xyz
)
)
)
[1] => Array
(
[post_id] => 5083755138419558
[text] => Carry on the Trend!
[links] => Array
(
[0] => Array
(
[link] => https:// .........
[text] => xyz
)
[1] => Array
(
[link] => https:// .........
[text] => xyz
)
)
)
[2] => .........
)
)
Controller Controller
foreach($array['posts'] as $scrapeData){
$product = new Product;
$product->id= $scrapeData['post_id'];
$product->description = $scrapeData['text'];
$product->save();
}
foreach($array['posts']['links'] as $datalink){
$images_links = new ProductImage;
$images_links->link= $datalink['link'];
$images_links->save();
}
You have to move your second loop into the first to iterate the links
:您必须将第二个循环移到第一个循环中以迭代
links
:
foreach($array['posts'] as $scrapeData){
$product = new Product;
$product->id= $scrapeData['post_id'];
$product->description = $scrapeData['text'];
$product->save();
foreach($scrapeData['links'] as $datalink) {
$images_links = new ProductImage;
$images_links->link= $datalink['link'];
$images_links->save();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.