How to search through nested JSON arrays in PHPHow to search through a JSON Array in PHPHow can I prevent SQL injection in PHP?How do I check if an array includes an object in JavaScript?How to append something to an array?How can I pretty-print JSON in a shell script?PHP: Delete an element from an arrayHow to insert an item into an array at a specific index (JavaScript)?How do I empty an array in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?

Knowledge-based authentication using Domain-driven Design in C#

How to stretch the corners of this image so that it looks like a perfect rectangle?

How seriously should I take size and weight limits of hand luggage?

Is there a hemisphere-neutral way of specifying a season?

Theorists sure want true answers to this!

How can saying a song's name be a copyright violation?

What historical events would have to change in order to make 19th century "steampunk" technology possible?

In the UK, is it possible to get a referendum by a court decision?

How badly should I try to prevent a user from XSSing themselves?

Processor speed limited at 0.4 Ghz

Forgetting the musical notes while performing in concert

Why didn't Boeing produce its own regional jet?

Can someone clarify Hamming's notion of important problems in relation to modern academia?

What is the most common color to indicate the input-field is disabled?

Avoiding the "not like other girls" trope?

How can a day be of 24 hours?

How does a dynamic QR code work?

Placement of More Information/Help Icon button for Radio Buttons

Rotate ASCII Art by 45 Degrees

In Bayesian inference, why are some terms dropped from the posterior predictive?

How to Prove P(a) → ∀x(P(x) ∨ ¬(x = a)) using Natural Deduction

Getting extremely large arrows with tikzcd

Does the Idaho Potato Commission associate potato skins with healthy eating?

How to install cross-compiler on Ubuntu 18.04?



How to search through nested JSON arrays in PHP


How to search through a JSON Array in PHPHow can I prevent SQL injection in PHP?How do I check if an array includes an object in JavaScript?How to append something to an array?How can I pretty-print JSON in a shell script?PHP: Delete an element from an arrayHow to insert an item into an array at a specific index (JavaScript)?How do I empty an array in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?













0















I'm sorry if I don't use the correct terminology, I'm still learning. I'm trying to figure out how to search a JSON array that has nested arrays in it for a specific value, and then return an associated value.



My problem is similar to this answered question on StackOverflow (How to search through a JSON Array in PHP), but I want to be able to find an item by id in either people or dog or any other nested array. Essentially I want to convert people below in the foreach to a wildcard.



Here is my JSON data - http://foothillertech.com/student/webdesign/2018/2018benrud2/tinker/dataIntro2/test/data.json



foreach($json->people as $item)

if($item->id == "8097")

echo $item->content;




Any help is greatly appreciated.










share|improve this question


























    0















    I'm sorry if I don't use the correct terminology, I'm still learning. I'm trying to figure out how to search a JSON array that has nested arrays in it for a specific value, and then return an associated value.



    My problem is similar to this answered question on StackOverflow (How to search through a JSON Array in PHP), but I want to be able to find an item by id in either people or dog or any other nested array. Essentially I want to convert people below in the foreach to a wildcard.



    Here is my JSON data - http://foothillertech.com/student/webdesign/2018/2018benrud2/tinker/dataIntro2/test/data.json



    foreach($json->people as $item)

    if($item->id == "8097")

    echo $item->content;




    Any help is greatly appreciated.










    share|improve this question
























      0












      0








      0








      I'm sorry if I don't use the correct terminology, I'm still learning. I'm trying to figure out how to search a JSON array that has nested arrays in it for a specific value, and then return an associated value.



      My problem is similar to this answered question on StackOverflow (How to search through a JSON Array in PHP), but I want to be able to find an item by id in either people or dog or any other nested array. Essentially I want to convert people below in the foreach to a wildcard.



      Here is my JSON data - http://foothillertech.com/student/webdesign/2018/2018benrud2/tinker/dataIntro2/test/data.json



      foreach($json->people as $item)

      if($item->id == "8097")

      echo $item->content;




      Any help is greatly appreciated.










      share|improve this question














      I'm sorry if I don't use the correct terminology, I'm still learning. I'm trying to figure out how to search a JSON array that has nested arrays in it for a specific value, and then return an associated value.



      My problem is similar to this answered question on StackOverflow (How to search through a JSON Array in PHP), but I want to be able to find an item by id in either people or dog or any other nested array. Essentially I want to convert people below in the foreach to a wildcard.



      Here is my JSON data - http://foothillertech.com/student/webdesign/2018/2018benrud2/tinker/dataIntro2/test/data.json



      foreach($json->people as $item)

      if($item->id == "8097")

      echo $item->content;




      Any help is greatly appreciated.







      php arrays json






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 7 at 21:29









      Mr. BMr. B

      1,04941529




      1,04941529






















          2 Answers
          2






          active

          oldest

          votes


















          1














          In this case, simply do two loops.



          $json = json_decode('
          "people": [

          "id": "8080",
          "content": "foo"
          ,

          "id": "8097",
          "content": "bar"

          ],
          "dogs": [

          "id": "8081",
          "content": "spot"
          ,

          "id": "8091",
          "content": "max"

          ]
          ');

          foreach($json as $key=>$value)
          //$key = people or dogs
          //$value = stdClass()

          foreach($value as $item)

          if($item->id == "8097")

          echo $item->content;





          Output



          bar


          Sandbox



          If we used 8081 instead of 8097 I would expect to get 'spot`. And testing that, I do indeed get that (which is in dogs).






          share|improve this answer























          • THANK YOU!!! Not only did you help me with my problem, but you also taught me the proper use as $key=>$value in a foreach. I've always thought that you have to ask for a specific item. If I'm correct, this method will allow me to pull any "key/value" pair from an array if I know its name. Thanks again!

            – Mr. B
            Mar 7 at 22:07


















          2














          If id is unique in the data, you can merge the inner arrays and index them by id.



          $data = json_decode($json, true);
          $merged = array_merge(...array_values($data));
          $indexed = array_column($merged, null, 'id');


          Then you can look up any of the items by id.



          echo $indexed['8097']['content'] ?? 'not found';


          This only works if id is unique. If not, indexing by id will result in data loss.






          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%2f55053066%2fhow-to-search-through-nested-json-arrays-in-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














            In this case, simply do two loops.



            $json = json_decode('
            "people": [

            "id": "8080",
            "content": "foo"
            ,

            "id": "8097",
            "content": "bar"

            ],
            "dogs": [

            "id": "8081",
            "content": "spot"
            ,

            "id": "8091",
            "content": "max"

            ]
            ');

            foreach($json as $key=>$value)
            //$key = people or dogs
            //$value = stdClass()

            foreach($value as $item)

            if($item->id == "8097")

            echo $item->content;





            Output



            bar


            Sandbox



            If we used 8081 instead of 8097 I would expect to get 'spot`. And testing that, I do indeed get that (which is in dogs).






            share|improve this answer























            • THANK YOU!!! Not only did you help me with my problem, but you also taught me the proper use as $key=>$value in a foreach. I've always thought that you have to ask for a specific item. If I'm correct, this method will allow me to pull any "key/value" pair from an array if I know its name. Thanks again!

              – Mr. B
              Mar 7 at 22:07















            1














            In this case, simply do two loops.



            $json = json_decode('
            "people": [

            "id": "8080",
            "content": "foo"
            ,

            "id": "8097",
            "content": "bar"

            ],
            "dogs": [

            "id": "8081",
            "content": "spot"
            ,

            "id": "8091",
            "content": "max"

            ]
            ');

            foreach($json as $key=>$value)
            //$key = people or dogs
            //$value = stdClass()

            foreach($value as $item)

            if($item->id == "8097")

            echo $item->content;





            Output



            bar


            Sandbox



            If we used 8081 instead of 8097 I would expect to get 'spot`. And testing that, I do indeed get that (which is in dogs).






            share|improve this answer























            • THANK YOU!!! Not only did you help me with my problem, but you also taught me the proper use as $key=>$value in a foreach. I've always thought that you have to ask for a specific item. If I'm correct, this method will allow me to pull any "key/value" pair from an array if I know its name. Thanks again!

              – Mr. B
              Mar 7 at 22:07













            1












            1








            1







            In this case, simply do two loops.



            $json = json_decode('
            "people": [

            "id": "8080",
            "content": "foo"
            ,

            "id": "8097",
            "content": "bar"

            ],
            "dogs": [

            "id": "8081",
            "content": "spot"
            ,

            "id": "8091",
            "content": "max"

            ]
            ');

            foreach($json as $key=>$value)
            //$key = people or dogs
            //$value = stdClass()

            foreach($value as $item)

            if($item->id == "8097")

            echo $item->content;





            Output



            bar


            Sandbox



            If we used 8081 instead of 8097 I would expect to get 'spot`. And testing that, I do indeed get that (which is in dogs).






            share|improve this answer













            In this case, simply do two loops.



            $json = json_decode('
            "people": [

            "id": "8080",
            "content": "foo"
            ,

            "id": "8097",
            "content": "bar"

            ],
            "dogs": [

            "id": "8081",
            "content": "spot"
            ,

            "id": "8091",
            "content": "max"

            ]
            ');

            foreach($json as $key=>$value)
            //$key = people or dogs
            //$value = stdClass()

            foreach($value as $item)

            if($item->id == "8097")

            echo $item->content;





            Output



            bar


            Sandbox



            If we used 8081 instead of 8097 I would expect to get 'spot`. And testing that, I do indeed get that (which is in dogs).







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 7 at 21:41









            ArtisticPhoenixArtisticPhoenix

            18.3k11226




            18.3k11226












            • THANK YOU!!! Not only did you help me with my problem, but you also taught me the proper use as $key=>$value in a foreach. I've always thought that you have to ask for a specific item. If I'm correct, this method will allow me to pull any "key/value" pair from an array if I know its name. Thanks again!

              – Mr. B
              Mar 7 at 22:07

















            • THANK YOU!!! Not only did you help me with my problem, but you also taught me the proper use as $key=>$value in a foreach. I've always thought that you have to ask for a specific item. If I'm correct, this method will allow me to pull any "key/value" pair from an array if I know its name. Thanks again!

              – Mr. B
              Mar 7 at 22:07
















            THANK YOU!!! Not only did you help me with my problem, but you also taught me the proper use as $key=>$value in a foreach. I've always thought that you have to ask for a specific item. If I'm correct, this method will allow me to pull any "key/value" pair from an array if I know its name. Thanks again!

            – Mr. B
            Mar 7 at 22:07





            THANK YOU!!! Not only did you help me with my problem, but you also taught me the proper use as $key=>$value in a foreach. I've always thought that you have to ask for a specific item. If I'm correct, this method will allow me to pull any "key/value" pair from an array if I know its name. Thanks again!

            – Mr. B
            Mar 7 at 22:07













            2














            If id is unique in the data, you can merge the inner arrays and index them by id.



            $data = json_decode($json, true);
            $merged = array_merge(...array_values($data));
            $indexed = array_column($merged, null, 'id');


            Then you can look up any of the items by id.



            echo $indexed['8097']['content'] ?? 'not found';


            This only works if id is unique. If not, indexing by id will result in data loss.






            share|improve this answer



























              2














              If id is unique in the data, you can merge the inner arrays and index them by id.



              $data = json_decode($json, true);
              $merged = array_merge(...array_values($data));
              $indexed = array_column($merged, null, 'id');


              Then you can look up any of the items by id.



              echo $indexed['8097']['content'] ?? 'not found';


              This only works if id is unique. If not, indexing by id will result in data loss.






              share|improve this answer

























                2












                2








                2







                If id is unique in the data, you can merge the inner arrays and index them by id.



                $data = json_decode($json, true);
                $merged = array_merge(...array_values($data));
                $indexed = array_column($merged, null, 'id');


                Then you can look up any of the items by id.



                echo $indexed['8097']['content'] ?? 'not found';


                This only works if id is unique. If not, indexing by id will result in data loss.






                share|improve this answer













                If id is unique in the data, you can merge the inner arrays and index them by id.



                $data = json_decode($json, true);
                $merged = array_merge(...array_values($data));
                $indexed = array_column($merged, null, 'id');


                Then you can look up any of the items by id.



                echo $indexed['8097']['content'] ?? 'not found';


                This only works if id is unique. If not, indexing by id will result in data loss.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 7 at 22:06









                Don't PanicDon't Panic

                30.1k94159




                30.1k94159



























                    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%2f55053066%2fhow-to-search-through-nested-json-arrays-in-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

                    1928 у кіно

                    Захаров Федір Захарович

                    Ель Греко