繁体   English   中英

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

[英]ErrorException Array to string conversion Laravel 8

我有 JSON 格式的数据,它被转换成数组。 但是我无法迭代数组并获取数据,它显示错误。

ErrorException 数组到字符串的转换

数据:

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

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();
    }

您必须将第二个循环移到第一个循环中以迭代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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM