How to fetch the last 5 data's from JSON using PHP?












-1















past 1 hour am struggling with this task, actually, I have to fetch the last 5 updated data from members.json.



Actually, I have one form if we submit that form values, that values are stored in a members.json page with JSON format.



Now how to fetch the postdate value in loop condition using PHP, I updated my JSON and index.php code below. Thanks advance



members.json



    [
{
"id": 1,
"urli": "test1",
"title": "test1",
"content": "Test1Test1Test1Test1",
"postdate": "December 31, 2018",
"hashtags": "#Christ #Love",
"image": "test1"
},
{
"id": 2,
"urli": "test2",
"title": "test2",
"content": "Test2Test2Test2Test2Test2",
"postdate": "December 31, 2018",
"hashtags": "#Faith Test2",
"image": "test2"
},
{
"id": 3,
"urli": "test3",
"title": "Test3",
"content": "Test3Test3Test3Test3",
"postdate": "December 31, 2018",
"hashtags": "#Faith #Christ",
"image": "test3"
},
{
"id": 4,
"urli": "test4",
"title": "Test4",
"content": "Test4Test4Test4Test4",
"postdate": "December 31, 2018",
"hashtags": "#Sermons Test4Test4",
"image": "test4"
},
{
"id": 5,
"urli": "test5",
"title": "Test5",
"content": "Test4Test4Test5Test5Test5",
"postdate": "December 31, 2018",
"hashtags": "#Christ #Love Test5",
"image": "test5"
},
{
"id": 6,
"urli": "test6",
"title": "test6",
"content": "Test6Test6Test6Test6Test6",
"postdate": "December 31, 2018",
"hashtags": "#Sermons #Faith #Christ #Love",
"image": "test6"
},
{
"id": 7,
"urli": "test7",
"title": "Test7",
"content": "Test7Test7Test7Test7",
"postdate": "December 31, 2018",
"hashtags": "#Sermons #Faith #Christ #Love",
"image": "test7"
},
{
"id": 8,
"urli": "test8",
"title": "Test8",
"content": "Test8Test8Test8Test8Test8",
"postdate": "December 31, 2018",
"hashtags": "#Sermons #Faith #Christ #Love",
"image": "test8"
},
{
"id": 9,
"urli": "test8",
"title": "Test8",
"content": "Test8Test8Test8Test8Test8",
"postdate": "jan 01, 2019",
"hashtags": "#Sermons #Faith #Christ #Love",
"image": "test8"
}
]


index.php



<div class="padding">
<h1>
<a href="single.html"></a>
</h1>
<div class="detail">
<div class="time">December 22, 2016</div>
</div>
</div>









share|improve this question




















  • 1





    Please show us what you have tried so far!

    – Joseph_J
    Jan 2 at 5:41






  • 1





    Did you try something? In fact - you've to decode your json, then sort(probably by ID) and iterate over it.

    – D. Dimitrov
    Jan 2 at 5:44






  • 1





    All of your postdate fields are the same. You cannot determine any update order unless storing the micro time.

    – Quasimodo's clone
    Jan 2 at 6:00













  • array_slice($array, -5, 5) if they're stored in order already.

    – mario
    Jan 2 at 6:03











  • -mario @ thank you, will try and lets see. that members.json page json have to updated day by day

    – angel
    Jan 2 at 6:04


















-1















past 1 hour am struggling with this task, actually, I have to fetch the last 5 updated data from members.json.



Actually, I have one form if we submit that form values, that values are stored in a members.json page with JSON format.



Now how to fetch the postdate value in loop condition using PHP, I updated my JSON and index.php code below. Thanks advance



members.json



    [
{
"id": 1,
"urli": "test1",
"title": "test1",
"content": "Test1Test1Test1Test1",
"postdate": "December 31, 2018",
"hashtags": "#Christ #Love",
"image": "test1"
},
{
"id": 2,
"urli": "test2",
"title": "test2",
"content": "Test2Test2Test2Test2Test2",
"postdate": "December 31, 2018",
"hashtags": "#Faith Test2",
"image": "test2"
},
{
"id": 3,
"urli": "test3",
"title": "Test3",
"content": "Test3Test3Test3Test3",
"postdate": "December 31, 2018",
"hashtags": "#Faith #Christ",
"image": "test3"
},
{
"id": 4,
"urli": "test4",
"title": "Test4",
"content": "Test4Test4Test4Test4",
"postdate": "December 31, 2018",
"hashtags": "#Sermons Test4Test4",
"image": "test4"
},
{
"id": 5,
"urli": "test5",
"title": "Test5",
"content": "Test4Test4Test5Test5Test5",
"postdate": "December 31, 2018",
"hashtags": "#Christ #Love Test5",
"image": "test5"
},
{
"id": 6,
"urli": "test6",
"title": "test6",
"content": "Test6Test6Test6Test6Test6",
"postdate": "December 31, 2018",
"hashtags": "#Sermons #Faith #Christ #Love",
"image": "test6"
},
{
"id": 7,
"urli": "test7",
"title": "Test7",
"content": "Test7Test7Test7Test7",
"postdate": "December 31, 2018",
"hashtags": "#Sermons #Faith #Christ #Love",
"image": "test7"
},
{
"id": 8,
"urli": "test8",
"title": "Test8",
"content": "Test8Test8Test8Test8Test8",
"postdate": "December 31, 2018",
"hashtags": "#Sermons #Faith #Christ #Love",
"image": "test8"
},
{
"id": 9,
"urli": "test8",
"title": "Test8",
"content": "Test8Test8Test8Test8Test8",
"postdate": "jan 01, 2019",
"hashtags": "#Sermons #Faith #Christ #Love",
"image": "test8"
}
]


index.php



<div class="padding">
<h1>
<a href="single.html"></a>
</h1>
<div class="detail">
<div class="time">December 22, 2016</div>
</div>
</div>









share|improve this question




















  • 1





    Please show us what you have tried so far!

    – Joseph_J
    Jan 2 at 5:41






  • 1





    Did you try something? In fact - you've to decode your json, then sort(probably by ID) and iterate over it.

    – D. Dimitrov
    Jan 2 at 5:44






  • 1





    All of your postdate fields are the same. You cannot determine any update order unless storing the micro time.

    – Quasimodo's clone
    Jan 2 at 6:00













  • array_slice($array, -5, 5) if they're stored in order already.

    – mario
    Jan 2 at 6:03











  • -mario @ thank you, will try and lets see. that members.json page json have to updated day by day

    – angel
    Jan 2 at 6:04
















-1












-1








-1








past 1 hour am struggling with this task, actually, I have to fetch the last 5 updated data from members.json.



Actually, I have one form if we submit that form values, that values are stored in a members.json page with JSON format.



Now how to fetch the postdate value in loop condition using PHP, I updated my JSON and index.php code below. Thanks advance



members.json



    [
{
"id": 1,
"urli": "test1",
"title": "test1",
"content": "Test1Test1Test1Test1",
"postdate": "December 31, 2018",
"hashtags": "#Christ #Love",
"image": "test1"
},
{
"id": 2,
"urli": "test2",
"title": "test2",
"content": "Test2Test2Test2Test2Test2",
"postdate": "December 31, 2018",
"hashtags": "#Faith Test2",
"image": "test2"
},
{
"id": 3,
"urli": "test3",
"title": "Test3",
"content": "Test3Test3Test3Test3",
"postdate": "December 31, 2018",
"hashtags": "#Faith #Christ",
"image": "test3"
},
{
"id": 4,
"urli": "test4",
"title": "Test4",
"content": "Test4Test4Test4Test4",
"postdate": "December 31, 2018",
"hashtags": "#Sermons Test4Test4",
"image": "test4"
},
{
"id": 5,
"urli": "test5",
"title": "Test5",
"content": "Test4Test4Test5Test5Test5",
"postdate": "December 31, 2018",
"hashtags": "#Christ #Love Test5",
"image": "test5"
},
{
"id": 6,
"urli": "test6",
"title": "test6",
"content": "Test6Test6Test6Test6Test6",
"postdate": "December 31, 2018",
"hashtags": "#Sermons #Faith #Christ #Love",
"image": "test6"
},
{
"id": 7,
"urli": "test7",
"title": "Test7",
"content": "Test7Test7Test7Test7",
"postdate": "December 31, 2018",
"hashtags": "#Sermons #Faith #Christ #Love",
"image": "test7"
},
{
"id": 8,
"urli": "test8",
"title": "Test8",
"content": "Test8Test8Test8Test8Test8",
"postdate": "December 31, 2018",
"hashtags": "#Sermons #Faith #Christ #Love",
"image": "test8"
},
{
"id": 9,
"urli": "test8",
"title": "Test8",
"content": "Test8Test8Test8Test8Test8",
"postdate": "jan 01, 2019",
"hashtags": "#Sermons #Faith #Christ #Love",
"image": "test8"
}
]


index.php



<div class="padding">
<h1>
<a href="single.html"></a>
</h1>
<div class="detail">
<div class="time">December 22, 2016</div>
</div>
</div>









share|improve this question
















past 1 hour am struggling with this task, actually, I have to fetch the last 5 updated data from members.json.



Actually, I have one form if we submit that form values, that values are stored in a members.json page with JSON format.



Now how to fetch the postdate value in loop condition using PHP, I updated my JSON and index.php code below. Thanks advance



members.json



    [
{
"id": 1,
"urli": "test1",
"title": "test1",
"content": "Test1Test1Test1Test1",
"postdate": "December 31, 2018",
"hashtags": "#Christ #Love",
"image": "test1"
},
{
"id": 2,
"urli": "test2",
"title": "test2",
"content": "Test2Test2Test2Test2Test2",
"postdate": "December 31, 2018",
"hashtags": "#Faith Test2",
"image": "test2"
},
{
"id": 3,
"urli": "test3",
"title": "Test3",
"content": "Test3Test3Test3Test3",
"postdate": "December 31, 2018",
"hashtags": "#Faith #Christ",
"image": "test3"
},
{
"id": 4,
"urli": "test4",
"title": "Test4",
"content": "Test4Test4Test4Test4",
"postdate": "December 31, 2018",
"hashtags": "#Sermons Test4Test4",
"image": "test4"
},
{
"id": 5,
"urli": "test5",
"title": "Test5",
"content": "Test4Test4Test5Test5Test5",
"postdate": "December 31, 2018",
"hashtags": "#Christ #Love Test5",
"image": "test5"
},
{
"id": 6,
"urli": "test6",
"title": "test6",
"content": "Test6Test6Test6Test6Test6",
"postdate": "December 31, 2018",
"hashtags": "#Sermons #Faith #Christ #Love",
"image": "test6"
},
{
"id": 7,
"urli": "test7",
"title": "Test7",
"content": "Test7Test7Test7Test7",
"postdate": "December 31, 2018",
"hashtags": "#Sermons #Faith #Christ #Love",
"image": "test7"
},
{
"id": 8,
"urli": "test8",
"title": "Test8",
"content": "Test8Test8Test8Test8Test8",
"postdate": "December 31, 2018",
"hashtags": "#Sermons #Faith #Christ #Love",
"image": "test8"
},
{
"id": 9,
"urli": "test8",
"title": "Test8",
"content": "Test8Test8Test8Test8Test8",
"postdate": "jan 01, 2019",
"hashtags": "#Sermons #Faith #Christ #Love",
"image": "test8"
}
]


index.php



<div class="padding">
<h1>
<a href="single.html"></a>
</h1>
<div class="detail">
<div class="time">December 22, 2016</div>
</div>
</div>






php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 6:04







angel

















asked Jan 2 at 5:33









angelangel

207




207








  • 1





    Please show us what you have tried so far!

    – Joseph_J
    Jan 2 at 5:41






  • 1





    Did you try something? In fact - you've to decode your json, then sort(probably by ID) and iterate over it.

    – D. Dimitrov
    Jan 2 at 5:44






  • 1





    All of your postdate fields are the same. You cannot determine any update order unless storing the micro time.

    – Quasimodo's clone
    Jan 2 at 6:00













  • array_slice($array, -5, 5) if they're stored in order already.

    – mario
    Jan 2 at 6:03











  • -mario @ thank you, will try and lets see. that members.json page json have to updated day by day

    – angel
    Jan 2 at 6:04
















  • 1





    Please show us what you have tried so far!

    – Joseph_J
    Jan 2 at 5:41






  • 1





    Did you try something? In fact - you've to decode your json, then sort(probably by ID) and iterate over it.

    – D. Dimitrov
    Jan 2 at 5:44






  • 1





    All of your postdate fields are the same. You cannot determine any update order unless storing the micro time.

    – Quasimodo's clone
    Jan 2 at 6:00













  • array_slice($array, -5, 5) if they're stored in order already.

    – mario
    Jan 2 at 6:03











  • -mario @ thank you, will try and lets see. that members.json page json have to updated day by day

    – angel
    Jan 2 at 6:04










1




1





Please show us what you have tried so far!

– Joseph_J
Jan 2 at 5:41





Please show us what you have tried so far!

– Joseph_J
Jan 2 at 5:41




1




1





Did you try something? In fact - you've to decode your json, then sort(probably by ID) and iterate over it.

– D. Dimitrov
Jan 2 at 5:44





Did you try something? In fact - you've to decode your json, then sort(probably by ID) and iterate over it.

– D. Dimitrov
Jan 2 at 5:44




1




1





All of your postdate fields are the same. You cannot determine any update order unless storing the micro time.

– Quasimodo's clone
Jan 2 at 6:00







All of your postdate fields are the same. You cannot determine any update order unless storing the micro time.

– Quasimodo's clone
Jan 2 at 6:00















array_slice($array, -5, 5) if they're stored in order already.

– mario
Jan 2 at 6:03





array_slice($array, -5, 5) if they're stored in order already.

– mario
Jan 2 at 6:03













-mario @ thank you, will try and lets see. that members.json page json have to updated day by day

– angel
Jan 2 at 6:04







-mario @ thank you, will try and lets see. that members.json page json have to updated day by day

– angel
Jan 2 at 6:04














2 Answers
2






active

oldest

votes


















1














After decoding the json data into array using array_slice last 5 values can be obtained.



$data= json_decode($data , true);
$values = array_slice($data, -5);


for reference:



http://php.net/manual/en/function.json-decode.php



https://www.w3schools.com/php/func_array_slice.asp






share|improve this answer































    3
















    $array = json_decode($data , true);

    $items = array_slice($array, -5);





    decode the json data to array then use array_slice.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001664%2fhow-to-fetch-the-last-5-datas-from-json-using-php%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      After decoding the json data into array using array_slice last 5 values can be obtained.



      $data= json_decode($data , true);
      $values = array_slice($data, -5);


      for reference:



      http://php.net/manual/en/function.json-decode.php



      https://www.w3schools.com/php/func_array_slice.asp






      share|improve this answer




























        1














        After decoding the json data into array using array_slice last 5 values can be obtained.



        $data= json_decode($data , true);
        $values = array_slice($data, -5);


        for reference:



        http://php.net/manual/en/function.json-decode.php



        https://www.w3schools.com/php/func_array_slice.asp






        share|improve this answer


























          1












          1








          1







          After decoding the json data into array using array_slice last 5 values can be obtained.



          $data= json_decode($data , true);
          $values = array_slice($data, -5);


          for reference:



          http://php.net/manual/en/function.json-decode.php



          https://www.w3schools.com/php/func_array_slice.asp






          share|improve this answer













          After decoding the json data into array using array_slice last 5 values can be obtained.



          $data= json_decode($data , true);
          $values = array_slice($data, -5);


          for reference:



          http://php.net/manual/en/function.json-decode.php



          https://www.w3schools.com/php/func_array_slice.asp







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 5 at 20:26









          SM Imtiaz HussainSM Imtiaz Hussain

          593




          593

























              3
















              $array = json_decode($data , true);

              $items = array_slice($array, -5);





              decode the json data to array then use array_slice.






              share|improve this answer




























                3
















                $array = json_decode($data , true);

                $items = array_slice($array, -5);





                decode the json data to array then use array_slice.






                share|improve this answer


























                  3












                  3








                  3









                  $array = json_decode($data , true);

                  $items = array_slice($array, -5);





                  decode the json data to array then use array_slice.






                  share|improve this answer















                  $array = json_decode($data , true);

                  $items = array_slice($array, -5);





                  decode the json data to array then use array_slice.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 7:04









                  sradhasradha

                  1,4121335




                  1,4121335






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001664%2fhow-to-fetch-the-last-5-datas-from-json-using-php%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      Npm cannot find a required file even through it is in the searched directory