Fetching key from an array of array?

Multi tool use
Multi tool use












0














I'm trying to parse this data from this simple array called $mission_data. 12654 and 12777 are keys to a long list of records. As an example i need to put the key 12654 into a variable so I can group them with their UserId's.



Array
(
[12654] => Array
(
[UserID] => 294
)
[12777] => Array
(
[UserID] => 320
)
)


My code looks like this:



foreach($mission_data as $data){
$lloc_key = key($mission_data);
$user_ID = $data['UserID'];
echo 'Location ID ='.$lloc_key."<br>";
echo 'User ID ='.$user_ID;}


The result I get back is:



Location ID =12654
User ID =294

Location ID =12654
User ID =320


If I replace $mission_data in the second line of that foreach loop with $data, I get the below:



Location ID =UserID
User ID =294

Location ID =UserID
User ID =320


I can't seem to get the key back from the first level of the array (and have it advance with the foreach loop. What am I doing wrong?



I know this is 101, but I'm stuck.



Thanks.










share|improve this question









New contributor




Steve Flynn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0














    I'm trying to parse this data from this simple array called $mission_data. 12654 and 12777 are keys to a long list of records. As an example i need to put the key 12654 into a variable so I can group them with their UserId's.



    Array
    (
    [12654] => Array
    (
    [UserID] => 294
    )
    [12777] => Array
    (
    [UserID] => 320
    )
    )


    My code looks like this:



    foreach($mission_data as $data){
    $lloc_key = key($mission_data);
    $user_ID = $data['UserID'];
    echo 'Location ID ='.$lloc_key."<br>";
    echo 'User ID ='.$user_ID;}


    The result I get back is:



    Location ID =12654
    User ID =294

    Location ID =12654
    User ID =320


    If I replace $mission_data in the second line of that foreach loop with $data, I get the below:



    Location ID =UserID
    User ID =294

    Location ID =UserID
    User ID =320


    I can't seem to get the key back from the first level of the array (and have it advance with the foreach loop. What am I doing wrong?



    I know this is 101, but I'm stuck.



    Thanks.










    share|improve this question









    New contributor




    Steve Flynn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0







      I'm trying to parse this data from this simple array called $mission_data. 12654 and 12777 are keys to a long list of records. As an example i need to put the key 12654 into a variable so I can group them with their UserId's.



      Array
      (
      [12654] => Array
      (
      [UserID] => 294
      )
      [12777] => Array
      (
      [UserID] => 320
      )
      )


      My code looks like this:



      foreach($mission_data as $data){
      $lloc_key = key($mission_data);
      $user_ID = $data['UserID'];
      echo 'Location ID ='.$lloc_key."<br>";
      echo 'User ID ='.$user_ID;}


      The result I get back is:



      Location ID =12654
      User ID =294

      Location ID =12654
      User ID =320


      If I replace $mission_data in the second line of that foreach loop with $data, I get the below:



      Location ID =UserID
      User ID =294

      Location ID =UserID
      User ID =320


      I can't seem to get the key back from the first level of the array (and have it advance with the foreach loop. What am I doing wrong?



      I know this is 101, but I'm stuck.



      Thanks.










      share|improve this question









      New contributor




      Steve Flynn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I'm trying to parse this data from this simple array called $mission_data. 12654 and 12777 are keys to a long list of records. As an example i need to put the key 12654 into a variable so I can group them with their UserId's.



      Array
      (
      [12654] => Array
      (
      [UserID] => 294
      )
      [12777] => Array
      (
      [UserID] => 320
      )
      )


      My code looks like this:



      foreach($mission_data as $data){
      $lloc_key = key($mission_data);
      $user_ID = $data['UserID'];
      echo 'Location ID ='.$lloc_key."<br>";
      echo 'User ID ='.$user_ID;}


      The result I get back is:



      Location ID =12654
      User ID =294

      Location ID =12654
      User ID =320


      If I replace $mission_data in the second line of that foreach loop with $data, I get the below:



      Location ID =UserID
      User ID =294

      Location ID =UserID
      User ID =320


      I can't seem to get the key back from the first level of the array (and have it advance with the foreach loop. What am I doing wrong?



      I know this is 101, but I'm stuck.



      Thanks.







      php multidimensional-array foreach key






      share|improve this question









      New contributor




      Steve Flynn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Steve Flynn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Dec 27 at 13:29









      Atul Sharma

      3,42442437




      3,42442437






      New contributor




      Steve Flynn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Dec 27 at 12:46









      Steve Flynn

      31




      31




      New contributor




      Steve Flynn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Steve Flynn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Steve Flynn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          foreach ($mission_data as $key => $data) {
          $user_ID = $data['UserID'];
          echo 'Location ID =' . $key . "<br>";
          echo 'User ID =' . $user_ID;
          }


          You can use the $key => $value of foreach loop to get the key of your array you try to access instantly.






          share|improve this answer





















          • Thank you for this quick response. Can I ask, is the $key variable a way to retrieve the key? Is that just a function within PHP? I'm not sure I understand how $key retrieves that, unless it's a feature. :)
            – Steve Flynn
            Dec 27 at 13:01










          • It's a feature of foreach loop. Your first approach was not so wrong, with small modifications it would work but the way i propose is faster using less code. If i helped you solve your issue please don't forget to accept my answer for future users to see.
            – pr1nc3
            Dec 27 at 13:03












          • great will do... it worked a charm. Thank you.
            – Steve Flynn
            Dec 27 at 13:44










          • Glad i helped :)
            – pr1nc3
            Dec 27 at 13:55











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


          }
          });






          Steve Flynn is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945355%2ffetching-key-from-an-array-of-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          foreach ($mission_data as $key => $data) {
          $user_ID = $data['UserID'];
          echo 'Location ID =' . $key . "<br>";
          echo 'User ID =' . $user_ID;
          }


          You can use the $key => $value of foreach loop to get the key of your array you try to access instantly.






          share|improve this answer





















          • Thank you for this quick response. Can I ask, is the $key variable a way to retrieve the key? Is that just a function within PHP? I'm not sure I understand how $key retrieves that, unless it's a feature. :)
            – Steve Flynn
            Dec 27 at 13:01










          • It's a feature of foreach loop. Your first approach was not so wrong, with small modifications it would work but the way i propose is faster using less code. If i helped you solve your issue please don't forget to accept my answer for future users to see.
            – pr1nc3
            Dec 27 at 13:03












          • great will do... it worked a charm. Thank you.
            – Steve Flynn
            Dec 27 at 13:44










          • Glad i helped :)
            – pr1nc3
            Dec 27 at 13:55
















          0














          foreach ($mission_data as $key => $data) {
          $user_ID = $data['UserID'];
          echo 'Location ID =' . $key . "<br>";
          echo 'User ID =' . $user_ID;
          }


          You can use the $key => $value of foreach loop to get the key of your array you try to access instantly.






          share|improve this answer





















          • Thank you for this quick response. Can I ask, is the $key variable a way to retrieve the key? Is that just a function within PHP? I'm not sure I understand how $key retrieves that, unless it's a feature. :)
            – Steve Flynn
            Dec 27 at 13:01










          • It's a feature of foreach loop. Your first approach was not so wrong, with small modifications it would work but the way i propose is faster using less code. If i helped you solve your issue please don't forget to accept my answer for future users to see.
            – pr1nc3
            Dec 27 at 13:03












          • great will do... it worked a charm. Thank you.
            – Steve Flynn
            Dec 27 at 13:44










          • Glad i helped :)
            – pr1nc3
            Dec 27 at 13:55














          0












          0








          0






          foreach ($mission_data as $key => $data) {
          $user_ID = $data['UserID'];
          echo 'Location ID =' . $key . "<br>";
          echo 'User ID =' . $user_ID;
          }


          You can use the $key => $value of foreach loop to get the key of your array you try to access instantly.






          share|improve this answer












          foreach ($mission_data as $key => $data) {
          $user_ID = $data['UserID'];
          echo 'Location ID =' . $key . "<br>";
          echo 'User ID =' . $user_ID;
          }


          You can use the $key => $value of foreach loop to get the key of your array you try to access instantly.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 27 at 12:51









          pr1nc3

          3,6152521




          3,6152521












          • Thank you for this quick response. Can I ask, is the $key variable a way to retrieve the key? Is that just a function within PHP? I'm not sure I understand how $key retrieves that, unless it's a feature. :)
            – Steve Flynn
            Dec 27 at 13:01










          • It's a feature of foreach loop. Your first approach was not so wrong, with small modifications it would work but the way i propose is faster using less code. If i helped you solve your issue please don't forget to accept my answer for future users to see.
            – pr1nc3
            Dec 27 at 13:03












          • great will do... it worked a charm. Thank you.
            – Steve Flynn
            Dec 27 at 13:44










          • Glad i helped :)
            – pr1nc3
            Dec 27 at 13:55


















          • Thank you for this quick response. Can I ask, is the $key variable a way to retrieve the key? Is that just a function within PHP? I'm not sure I understand how $key retrieves that, unless it's a feature. :)
            – Steve Flynn
            Dec 27 at 13:01










          • It's a feature of foreach loop. Your first approach was not so wrong, with small modifications it would work but the way i propose is faster using less code. If i helped you solve your issue please don't forget to accept my answer for future users to see.
            – pr1nc3
            Dec 27 at 13:03












          • great will do... it worked a charm. Thank you.
            – Steve Flynn
            Dec 27 at 13:44










          • Glad i helped :)
            – pr1nc3
            Dec 27 at 13:55
















          Thank you for this quick response. Can I ask, is the $key variable a way to retrieve the key? Is that just a function within PHP? I'm not sure I understand how $key retrieves that, unless it's a feature. :)
          – Steve Flynn
          Dec 27 at 13:01




          Thank you for this quick response. Can I ask, is the $key variable a way to retrieve the key? Is that just a function within PHP? I'm not sure I understand how $key retrieves that, unless it's a feature. :)
          – Steve Flynn
          Dec 27 at 13:01












          It's a feature of foreach loop. Your first approach was not so wrong, with small modifications it would work but the way i propose is faster using less code. If i helped you solve your issue please don't forget to accept my answer for future users to see.
          – pr1nc3
          Dec 27 at 13:03






          It's a feature of foreach loop. Your first approach was not so wrong, with small modifications it would work but the way i propose is faster using less code. If i helped you solve your issue please don't forget to accept my answer for future users to see.
          – pr1nc3
          Dec 27 at 13:03














          great will do... it worked a charm. Thank you.
          – Steve Flynn
          Dec 27 at 13:44




          great will do... it worked a charm. Thank you.
          – Steve Flynn
          Dec 27 at 13:44












          Glad i helped :)
          – pr1nc3
          Dec 27 at 13:55




          Glad i helped :)
          – pr1nc3
          Dec 27 at 13:55










          Steve Flynn is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Steve Flynn is a new contributor. Be nice, and check out our Code of Conduct.













          Steve Flynn is a new contributor. Be nice, and check out our Code of Conduct.












          Steve Flynn is a new contributor. Be nice, and check out our Code of Conduct.
















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53945355%2ffetching-key-from-an-array-of-array%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







          SY15B9GZdA9YVzKTbN szyiZpP16tfMyU,dY jegxfkIgq nexN,xFeu78 DBdxQv8fO3Npx4IWAvSPQ 5D3fnzko8cr
          L,qufr,zvv8m5fb9HwmuFu3HkW

          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas