mercredi 24 février 2016

Get attributes from a JSON array in Laravel

I have a piece of code that returns an array that looks like this.

    Collection {#195 ▼
  #items: array:2 [▼
    "kind" => "Listing"
    "data" => array:5 [▼
      "facets" => []
      "modhash" => ""
      "children" => array:25 [▼
        0 => array:2 [▼
          "kind" => "t3"
          "data" => array:52 [▼
            "domain" => "self.apple"
            "banned_by" => null
            "media_embed" => []
            "subreddit" => "apple"
            "selftext_html" => """
              <!-- SC_OFF --><div class="md"><p><strong>MEGA EDIT:</strong> Please read before asking questions, as most things people asking me are repeats:</p>\n
              \n
              <p><strong>Q:</strong> Can't you just distribute the extension yourself?</p>\n
              \n
              <p><strong>A:</strong> I already do. However, it seems from Apple's email to all Safari extension developers that we must pay to continue supporting our extensions and providing updates. A couple of users have linked to articles that give confusing information about whether or not this is really the case. <a href="http://ift.tt/1MnfVih">here is one of them</a>, which confusingly states that the developer of a popular extension will pay the fee "to ensure that his extension will still be available for El Capitan users."</p>\n
              \n
              <p>From <a href="http://ift.tt/1MnfVij">another article</a>, it seems that perhaps I could still "release" RES on my own without paying apple - but auto update functionality would go away. This is pretty much a dealbreaker for any browser extension that interacts with a website, as websites change somewhat often, and a developer definitely can't count on people to update their extensions manually.</p>\n
              \n
              <p>If in fact this is all a result of a poorly worded email, then I will be thrilled that all Apple is "guilty of" here is doing a crappy job with the email they sent me. Here's the relevant text of Apple's email to me which leads me to believe I must pay the fee to continue giving people updates to RES:</p>\n
              \n
              <blockquote>\n
              <p>You can continue building Safari extensions and bring your creativity to other Apple platforms by joining the Apple Developer Program. Join today to provide updates to your current extensions, build new extensions, and submit your extensions to the new Safari Extensions Gallery for OS X El Capitan.</p>\n
              </blockquote>\n
              \n
              <p>(joining the program is what costs $100 per year)</p>\n
              \n
              <hr/>\n
              \n
              <p><strong>Q:</strong> It's to keep spammers out, idiot.</p>\n
              \n
              <p><strong>A:</strong> That's not really a question. Also, there's no real evidence that that's why they're doing this. Furthermore, it's worth way more than $100 to get malware/spam installed into many users' browsers. $100 isn't much of a deterrent. I don't think that's really the reason. It seems the real reason is just that they've consolidated their 3 separate developer programs (iOS / OSX / Safari Extensions) for simplicity's sake, but not properly thought about how that might upset / affect people who were only interested in building Safari Extensions (which was previously free) and not the other two.</p>\n
              \n
              <hr/>\n
              \n
              <p><strong>Q:</strong> You can't come up with $100? What are you poor or something?</p>\n
              \n
              <p><strong>A:</strong> I'm far less concerned about my own ability to come up with $100 than I am about developers in general being shut out from the system over this. Not everyone has the user base that RES has.  </p>\n
              \n
              <hr/>\n
              \n
              <p><strong>Q:</strong> But you get a lot of stuff for that $100 per year. What are you complaining about?</p>\n
              \n
              <p><strong>A:</strong> Safari (on Desktop) is a browser with just 5% market share, and paying $100 just to build extensions for it doesn't seem wise, especially when people expect extensions to be free.  Apple announced Swift was open source, and then makes this move that I feel hurts open source developers.  Sure, the iOS SDK and Xcode are great, and probably worth $100 -- but only to people who are going to develop iOS or OSX applications. I'm not, so those have no value to me.</p>\n
              \n
              <hr/>\n
              \n
              <p><strong>Q:</strong> Why do you think Apple is doing this? Do you really think they're trying to hurt extension devs?</p>\n
              \n
              <p><strong>A:</strong> I honestly think they just didn't think about it too much. I think they made a business decision to consolidate their developer programs - one that generally makes sense - and it didn't occur to them that people who are <em>only</em> developing extensions might be upset about this.  That, or the articles above are correct and the email I got was just misleading / poorly written.</p>\n
              \n
              <hr/>\n
              \n
              <p><strong>Q:</strong> If I give you $100 does this problem go away?</p>\n
              \n
              <p><strong>A:</strong> My goal here, although I very much appreciate people's generous offers to help pay for it, is to raise awareness and hopefully get more open source developers to <a href="http://ift.tt/1ay6ISf"><strong>politely</strong> provide feedback to Apple</a> that this policy is not OK.  Sure I could pay for it with donations you guys give me - but then other open source developers who haven't yet gained a following that will help pay are still walled out by this $100 fee.</p>\n
              \n
              <p>If you're not a developer but still want to give <em>polite</em> feedback from the perspective of a user, <a href="http://ift.tt/1vmrcXx">here's the general safari feedback page</a> </p>\n
              \n
              <p>The original post:</p>\n
              \n
              <hr/>\n
              \n
              <p>So it used to be free to be a part of the Safari developer program. That's being folded into Apple's dev program now, and I'm required to pay $100 to join if I want to continue publishing Reddit Enhancement Suite - which is free.</p>\n
              \n
              <p>$100 would be several months worth of donations, on many/most months, and only to support less than 1% of RES users (as in, Safari makes somewhere around 1%).</p>\n
              \n
              <p>Not only is the cost an annoyance, I also don't feel Apple deserves $100 from me just so I can have the privilege of continuing to publish <em>free</em> software that enhances its browsers.  They're not providing a value add here (e.g. the iOS SDK, etc) that justifies charging us money.  </p>\n
              \n
              <p>To be clear: RES isn't published on their extension gallery, so the $100 being allocated to their "review process" isn't really valid either. In addition, spammers / malicious extension developers have a lot more than $100 to gain from publishing scammy apps.  My Safari developer certificate is already linked / provided through my iTunes account ID (and therefore credit card etc), so it's not like the $100 gets them "more confirmation" that I am who I say I am.</p>\n
              \n
              <p>I don't know what I'm going to do yet, but worst case scenario I will try my best to get one more release out before the deadline screws me (and therefore you, if you use Safari/RES) over.</p>\n
              </div><!-- SC_ON -->
              """
            "selftext" => """
              **MEGA EDIT:** Please read before asking questions, as most things people asking me are repeats:\n
              \n
              **Q:** Can't you just distribute the extension yourself?\n
              \n
              **A:** I already do. However, it seems from Apple's email to all Safari extension developers that we must pay to continue supporting our extensions and providing updates. A couple of users have linked to articles that give confusing information about whether or not this is really the case. [here is one of them](http://ift.tt/1MnfVih), which confusingly states that the developer of a popular extension will pay the fee "to ensure that his extension will still be available for El Capitan users."\n
              \n
              From [another article](http://ift.tt/1MnfVij), it seems that perhaps I could still "release" RES on my own without paying apple - but auto update functionality would go away. This is pretty much a dealbreaker for any browser extension that interacts with a website, as websites change somewhat often, and a developer definitely can't count on people to update their extensions manually.\n
              \n
              If in fact this is all a result of a poorly worded email, then I will be thrilled that all Apple is "guilty of" here is doing a crappy job with the email they sent me. Here's the relevant text of Apple's email to me which leads me to believe I must pay the fee to continue giving people updates to RES:\n
              \n
              > You can continue building Safari extensions and bring your creativity to other Apple platforms by joining the Apple Developer Program. Join today to provide updates to your current extensions, build new extensions, and submit your extensions to the new Safari Extensions Gallery for OS X El Capitan.\n
              \n
              (joining the program is what costs $100 per year)\n
              \n
              ---\n
              \n
              **Q:** It's to keep spammers out, idiot.\n
              \n
              **A:** That's not really a question. Also, there's no real evidence that that's why they're doing this. Furthermore, it's worth way more than $100 to get malware/spam installed into many users' browsers. $100 isn't much of a deterrent. I don't think that's really the reason. It seems the real reason is just that they've consolidated their 3 separate developer programs (iOS / OSX / Safari Extensions) for simplicity's sake, but not properly thought about how that might upset / affect people who were only interested in building Safari Extensions (which was previously free) and not the other two.\n
              \n
              ---\n
              \n
              **Q:** You can't come up with $100? What are you poor or something?\n
              \n
              **A:** I'm far less concerned about my own ability to come up with $100 than I am about developers in general being shut out from the system over this. Not everyone has the user base that RES has.  \n
              \n
              ---\n
              \n
              **Q:** But you get a lot of stuff for that $100 per year. What are you complaining about?\n
              \n
              **A:** Safari (on Desktop) is a browser with just 5% market share, and paying $100 just to build extensions for it doesn't seem wise, especially when people expect extensions to be free.  Apple announced Swift was open source, and then makes this move that I feel hurts open source developers.  Sure, the iOS SDK and Xcode are great, and probably worth $100 -- but only to people who are going to develop iOS or OSX applications. I'm not, so those have no value to me.\n
              \n
              ---\n
              \n
              **Q:** Why do you think Apple is doing this? Do you really think they're trying to hurt extension devs?\n
              \n
              **A:** I honestly think they just didn't think about it too much. I think they made a business decision to consolidate their developer programs - one that generally makes sense - and it didn't occur to them that people who are *only* developing extensions might be upset about this.  That, or the articles above are correct and the email I got was just misleading / poorly written.\n
              \n
              ---\n
              \n
              **Q:** If I give you $100 does this problem go away?\n
              \n
              **A:** My goal here, although I very much appreciate people's generous offers to help pay for it, is to raise awareness and hopefully get more open source developers to [**politely** provide feedback to Apple](http://ift.tt/1ay6ISf) that this policy is not OK.  Sure I could pay for it with donations you guys give me - but then other open source developers who haven't yet gained a following that will help pay are still walled out by this $100 fee.\n
              \n
              If you're not a developer but still want to give *polite* feedback from the perspective of a user, [here's the general safari feedback page](http://ift.tt/1vmrcXx) \n
              \n
              \n
              The original post:\n
              \n
              -----\n
              \n
              So it used to be free to be a part of the Safari developer program. That's being folded into Apple's dev program now, and I'm required to pay $100 to join if I want to continue publishing Reddit Enhancement Suite - which is free.\n
              \n
              $100 would be several months worth of donations, on many/most months, and only to support less than 1% of RES users (as in, Safari makes somewhere around 1%).\n
              \n
              Not only is the cost an annoyance, I also don't feel Apple deserves $100 from me just so I can have the privilege of continuing to publish *free* software that enhances its browsers.  They're not providing a value add here (e.g. the iOS SDK, etc) that justifies charging us money.  \n
              \n
              To be clear: RES isn't published on their extension gallery, so the $100 being allocated to their "review process" isn't really valid either. In addition, spammers / malicious extension developers have a lot more than $100 to gain from publishing scammy apps.  My Safari developer certificate is already linked / provided through my iTunes account ID (and therefore credit card etc), so it's not like the $100 gets them "more confirmation" that I am who I say I am.\n
              \n
              I don't know what I'm going to do yet, but worst case scenario I will try my best to get one more release out before the deadline screws me (and therefore you, if you use Safari/RES) over.
              """
            "likes" => null
            "suggested_sort" => null
            "user_reports" => []
            "secure_media" => null
            "link_flair_text" => "Safari"
            "id" => "397bn6"
            "from_kind" => null
            "gilded" => 2
            "archived" => true
            "clicked" => false
            "report_reasons" => null
            "author" => "honestbleeps"
            "media" => null
            "score" => 6040
            "approved_by" => null
            "over_18" => false
            "hidden" => false
            "num_comments" => 1285
            "thumbnail" => ""
            "subreddit_id" => "t5_2qh1f"
            "hide_score" => false
            "edited" => 1433916555.0
            "link_flair_css_class" => "Safari"
            "author_flair_css_class" => null
            "downs" => 0
            "secure_media_embed" => []
            "saved" => false
            "removal_reason" => null
            "stickied" => false
            "from" => null
            "is_self" => true
            "from_id" => null
            "permalink" => "/r/apple/comments/397bn6/apple_wants_me_to_pay_100_to_continue_publishing/?ref=search_posts"
            "locked" => false
            "name" => "t3_397bn6"
            "created" => 1433908213.0
            "url" => "http://ift.tt/1KnQGhd"
            "author_flair_text" => null
            "quarantine" => false
            "title" => "Apple wants me to pay $100 to continue publishing my (free) Safari extension (Reddit Enhancement Suite)"
            "created_utc" => 1433879413.0
            "distinguished" => null
            "mod_reports" => []
            "visited" => false
            "num_reports" => null
            "ups" => 6040
          ]
        ]
        1 => array:2 [▶]
        2 => array:2 [▶]
        3 => array:2 [▶]
        4 => array:2 [▶]
        5 => array:2 [▶]
        6 => array:2 [▶]
        7 => array:2 [▶]
        8 => array:2 [▶]
        9 => array:2 [▶]

I get this result by doing this in Laravel 5.2

public function store(Requests\SearchRequest $request)
    {

        $search_phrase = $request->input('search');

        $client = new HttpClient;

        $response = $client->get('http://ift.tt/1OuzIve'. $search_phrase .'');

        $response = collect(json_decode($response->content(), true));

        dd($response);

    }
}

So I am using an HTTP client to make a call to reddit to get comments based on a search result. I have a form with a text field that I send to a controller where I request the input and then make a call to reddit using the $client and a get method. This then returns the results that I want in a decoded JSON array.

I only want to get the "title" & "created" attribute from the this array and make a new array that looks like this,

$post_info= ["title" => "After comparing Siri to the Google app, I feel like Apple isn't even trying.", "created" => 1450915864.0]

After I have done that I want to get all of the "selftext_html" and parse those into another array for every $post_info array.

This way I can then store the first array in the database with a foreach loop, then store all of the selftext_html in another table in the database with another foreach loop.

So I can have two tables, one for post_info and the other for comments_on_post.

My question is, how can I get information from this array into another array that I can use to store the data.

<script>
    $.getJSON("http://ift.tt/1KJYvRh", function foo(data) {

            var test = [];
            $.each(
                data.data.children.slice(0, 100),
                function (i, post) {


                    test.push(post.data.title);

                    $("#reddit-content").append( '<br>' + post.data.title );
                    $("#reddit-content").append( '<br>' + post.data.selftext_html );
                    $("#reddit-content").append( '<br>' + post.data.created);
                    $("#reddit-content").append( '<hr>' );
                }
            );

            console.dir(test);
            var toPhp = JSON.stringify(test);

            console.log(toPhp);
        }
    );
</script>

I thought to maybe try get the data with Jquery like I have done above but not sure how to return that back to an array in Laravel



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire