简体   繁体   English

ErrorException 数组到字符串的转换 Laravel 8

[英]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.

相关问题 带有转发器字段的 Laravel 表单提交中的 ErrorException 数组到字符串转换 - ErrorException Array to string conversion in Laravel form submission with repeater fields ErrorException 数组到字符串的转换 Laravel - 将 JSON 导入到 MySQL - ErrorException Array to string conversion Laravel - Importing JSON to MySQL Laravel 中的 ErrorException (E_NOTICE) 数组到字符串的转换 - ErrorException (E_NOTICE) Array to string conversion in Laravel 在 laravel 中上传图像时数组到字符串转换 ErrorException - Array to string conversion ErrorException while uploading image in laravel 7 使用Laravel和Lang :: get()进行代码接收“ ErrorException数组到字符串的转换” - Codeception “ErrorException array to string conversion” with Laravel and Lang::get() ErrorException:PHP中的数组到字符串的转换 - ErrorException : Array to string conversion in PHP 迁移--seed时将ErrorException数组转换为字符串 - ErrorException array to string conversion on migrate --seed ErrorException 数组到 Controller 文件中的字符串转换 - ErrorException Array to string conversion in Controller file ErrorException [注意]:数组到字符串转换PHP表单 - ErrorException [Notice]:Array to string conversion PHP Form 提交表单时出现“ErrorException 数组到字符串的转换” - "ErrorException Array to string conversion" when submitting a form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM