简体   繁体   English

在MySql数据库中将json_decode数据从最早的存储到最新

[英]storing json_decode data from oldest to newest in MySql database

I have this code: 我有以下代码:

$url = 'http://example.com/523223.json?';
        $json= file_get_contents($url);

        $data = json_decode($json);
        $rows = $data->{'items'};
        foreach ($rows as $row) {
            echo '<p>';
            $title = $row->name;
            $description = $row->description;
            $link = $row->link;
            $image_link = $row->thumbnails;

            $path = $image_link->large;
            $filename = basename($path);

            try {
                Image::make($path)->save(public_path('storage/posts/' . $filename));
            } catch (\Intervention\Image\Exception\NotReadableException $e) {
                Image::make($path)->save(public_path('storage/posts/' . 'https://lh3.googleusercontent.com/yI8LPDBhjvqLR1mQMitJlibZdWqaYAlMVUJK6zpBQkOb_Bk03qn_l2SQyn5yY__KZcY-=w300-rw'));
            }

            $post = new Post;
            $post->title = $title;
            $post->body = '..';
            $post->excerpt = $description;
            $post->meta_description = $link;
            $post->image = 'posts/' . $filename;

            $post->save();
        }

And what it does is grab content from JSON URL and store it in to a database which it's based on LaraveL. 它所做的是从JSON URL中获取内容并将其存储到基于LaraveL的数据库中。 so the issue is it's storing the data from newest to oldest but what I want to is store the items from oldest to newest. 所以问题是它从最新到最旧存储数据,但我要存储的项目是从最旧到最新。 That was the first question and the second question is how to make it store only the newest ones not duplicate those that are already stored and only check for those that are new only? 那是第一个问题,第二个问题是如何使它仅存储最新的而不重复已存储的,而仅检查仅新的?

Here's how it's done just by using array_reverse: 这是通过使用array_reverse来完成的:

$url = 'http://example.com/523223.json?';
        $json= file_get_contents($url);

        $data = json_decode($json);
        $rows = $data->{'items'};
        foreach (array_reverse($rows) as $row) {
            echo '<p>';
            $title = $row->name;
            $description = $row->description;
            $link = $row->link;
            $image_link = $row->thumbnail;

            $path = $image_link;
            $filename = basename($path);

            try {
                Image::make($path)->save(public_path('storage/posts/' . $filename));
            } catch (\Intervention\Image\Exception\NotReadableException $e) {
                Image::make($path)->save(public_path('storage/posts/' . 'https://lh3.googleusercontent.com/yI8LPDBhjvqLR1mQMitJlibZdWqaYAlMVUJK6zpBQkOb_Bk03qn_l2SQyn5yY__KZcY-=w300-rw'));
            }

            $post = new Post;
            $post->title = $title;
            $post->body = '..';
            $post->excerpt = $description;
            $post->meta_description = $link;
            $post->slug = $link;
            $post->image = 'posts/' . $filename;

            $post->save();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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