Get key from array with multiple values












0















So i need to return the number (6, 8 or 10) with the country value.
So in the example, with 'sweden' its supposed to return 8 but the key of the array is apparently just Array(). Is the wrong in the structure of my array or the usage of array_keys?



$list= array (
'list' =>
array (
6 =>
array (
'default',
'finland'
),
8 =>
array (
'sweden',
'norway'
),
10 =>
array (
'germany',
'belgia'
),
),
);
print_r(array_keys($list, "sweden"));


return: Array()










share|improve this question

























  • Possible duplicate of PHP Multidimensional Array Searching (Find key by specific value)

    – René Höhle
    Jan 1 at 1:26











  • doesnt look to be

    – justaguytrynabuildawebsite
    Jan 1 at 1:28











  • What? use the accepted Answer that is working ;) arrray_keys isn't working with multidimensional arrays very well.

    – René Höhle
    Jan 1 at 1:29











  • in that question its an associative array and is looking for 'slug' with slugs value, this is different

    – justaguytrynabuildawebsite
    Jan 1 at 1:32
















0















So i need to return the number (6, 8 or 10) with the country value.
So in the example, with 'sweden' its supposed to return 8 but the key of the array is apparently just Array(). Is the wrong in the structure of my array or the usage of array_keys?



$list= array (
'list' =>
array (
6 =>
array (
'default',
'finland'
),
8 =>
array (
'sweden',
'norway'
),
10 =>
array (
'germany',
'belgia'
),
),
);
print_r(array_keys($list, "sweden"));


return: Array()










share|improve this question

























  • Possible duplicate of PHP Multidimensional Array Searching (Find key by specific value)

    – René Höhle
    Jan 1 at 1:26











  • doesnt look to be

    – justaguytrynabuildawebsite
    Jan 1 at 1:28











  • What? use the accepted Answer that is working ;) arrray_keys isn't working with multidimensional arrays very well.

    – René Höhle
    Jan 1 at 1:29











  • in that question its an associative array and is looking for 'slug' with slugs value, this is different

    – justaguytrynabuildawebsite
    Jan 1 at 1:32














0












0








0








So i need to return the number (6, 8 or 10) with the country value.
So in the example, with 'sweden' its supposed to return 8 but the key of the array is apparently just Array(). Is the wrong in the structure of my array or the usage of array_keys?



$list= array (
'list' =>
array (
6 =>
array (
'default',
'finland'
),
8 =>
array (
'sweden',
'norway'
),
10 =>
array (
'germany',
'belgia'
),
),
);
print_r(array_keys($list, "sweden"));


return: Array()










share|improve this question
















So i need to return the number (6, 8 or 10) with the country value.
So in the example, with 'sweden' its supposed to return 8 but the key of the array is apparently just Array(). Is the wrong in the structure of my array or the usage of array_keys?



$list= array (
'list' =>
array (
6 =>
array (
'default',
'finland'
),
8 =>
array (
'sweden',
'norway'
),
10 =>
array (
'germany',
'belgia'
),
),
);
print_r(array_keys($list, "sweden"));


return: Array()







php arrays key






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 1:26







justaguytrynabuildawebsite

















asked Jan 1 at 1:12









justaguytrynabuildawebsitejustaguytrynabuildawebsite

106




106













  • Possible duplicate of PHP Multidimensional Array Searching (Find key by specific value)

    – René Höhle
    Jan 1 at 1:26











  • doesnt look to be

    – justaguytrynabuildawebsite
    Jan 1 at 1:28











  • What? use the accepted Answer that is working ;) arrray_keys isn't working with multidimensional arrays very well.

    – René Höhle
    Jan 1 at 1:29











  • in that question its an associative array and is looking for 'slug' with slugs value, this is different

    – justaguytrynabuildawebsite
    Jan 1 at 1:32



















  • Possible duplicate of PHP Multidimensional Array Searching (Find key by specific value)

    – René Höhle
    Jan 1 at 1:26











  • doesnt look to be

    – justaguytrynabuildawebsite
    Jan 1 at 1:28











  • What? use the accepted Answer that is working ;) arrray_keys isn't working with multidimensional arrays very well.

    – René Höhle
    Jan 1 at 1:29











  • in that question its an associative array and is looking for 'slug' with slugs value, this is different

    – justaguytrynabuildawebsite
    Jan 1 at 1:32

















Possible duplicate of PHP Multidimensional Array Searching (Find key by specific value)

– René Höhle
Jan 1 at 1:26





Possible duplicate of PHP Multidimensional Array Searching (Find key by specific value)

– René Höhle
Jan 1 at 1:26













doesnt look to be

– justaguytrynabuildawebsite
Jan 1 at 1:28





doesnt look to be

– justaguytrynabuildawebsite
Jan 1 at 1:28













What? use the accepted Answer that is working ;) arrray_keys isn't working with multidimensional arrays very well.

– René Höhle
Jan 1 at 1:29





What? use the accepted Answer that is working ;) arrray_keys isn't working with multidimensional arrays very well.

– René Höhle
Jan 1 at 1:29













in that question its an associative array and is looking for 'slug' with slugs value, this is different

– justaguytrynabuildawebsite
Jan 1 at 1:32





in that question its an associative array and is looking for 'slug' with slugs value, this is different

– justaguytrynabuildawebsite
Jan 1 at 1:32












3 Answers
3






active

oldest

votes


















3














You have two problems.



First, the array you want to search is $list['list'], not $list itself.



Second, the second argument to array_keys() is only useful for 1-dimensional arrays. You have a 2-dimensional array, but array_keys() will not automatically search inside the nested arrays. So you need to write your own loop or use array_filter().



$results = array();
foreach ($list['list'] as $key => $value) {
if (array_search('sweden', $value) !== false) {
$results = $key;
}
}
print_r($results);





share|improve this answer


























  • im getting a Parse error: syntax error, unexpected 'as' , do i need to edit something?

    – justaguytrynabuildawebsite
    Jan 1 at 1:38






  • 1





    Should be foreach, not for

    – Nick
    Jan 1 at 1:39



















0














use foreach for it.



foreach ($list as $key => $value){

}





share|improve this answer































    0














    I think this is what you want



    foreach($list as $key => $value ){  
    $arr = array_keys($value);//this has your (6, 8 or 10)
    foreach($arr as $val){
    print_r($value[$val]);//showing array data of 6,8,10 indexes
    }
    }


    output:
    enter image description here






    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%2f53992446%2fget-key-from-array-with-multiple-values%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      You have two problems.



      First, the array you want to search is $list['list'], not $list itself.



      Second, the second argument to array_keys() is only useful for 1-dimensional arrays. You have a 2-dimensional array, but array_keys() will not automatically search inside the nested arrays. So you need to write your own loop or use array_filter().



      $results = array();
      foreach ($list['list'] as $key => $value) {
      if (array_search('sweden', $value) !== false) {
      $results = $key;
      }
      }
      print_r($results);





      share|improve this answer


























      • im getting a Parse error: syntax error, unexpected 'as' , do i need to edit something?

        – justaguytrynabuildawebsite
        Jan 1 at 1:38






      • 1





        Should be foreach, not for

        – Nick
        Jan 1 at 1:39
















      3














      You have two problems.



      First, the array you want to search is $list['list'], not $list itself.



      Second, the second argument to array_keys() is only useful for 1-dimensional arrays. You have a 2-dimensional array, but array_keys() will not automatically search inside the nested arrays. So you need to write your own loop or use array_filter().



      $results = array();
      foreach ($list['list'] as $key => $value) {
      if (array_search('sweden', $value) !== false) {
      $results = $key;
      }
      }
      print_r($results);





      share|improve this answer


























      • im getting a Parse error: syntax error, unexpected 'as' , do i need to edit something?

        – justaguytrynabuildawebsite
        Jan 1 at 1:38






      • 1





        Should be foreach, not for

        – Nick
        Jan 1 at 1:39














      3












      3








      3







      You have two problems.



      First, the array you want to search is $list['list'], not $list itself.



      Second, the second argument to array_keys() is only useful for 1-dimensional arrays. You have a 2-dimensional array, but array_keys() will not automatically search inside the nested arrays. So you need to write your own loop or use array_filter().



      $results = array();
      foreach ($list['list'] as $key => $value) {
      if (array_search('sweden', $value) !== false) {
      $results = $key;
      }
      }
      print_r($results);





      share|improve this answer















      You have two problems.



      First, the array you want to search is $list['list'], not $list itself.



      Second, the second argument to array_keys() is only useful for 1-dimensional arrays. You have a 2-dimensional array, but array_keys() will not automatically search inside the nested arrays. So you need to write your own loop or use array_filter().



      $results = array();
      foreach ($list['list'] as $key => $value) {
      if (array_search('sweden', $value) !== false) {
      $results = $key;
      }
      }
      print_r($results);






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 1 at 1:41

























      answered Jan 1 at 1:34









      BarmarBarmar

      429k36253353




      429k36253353













      • im getting a Parse error: syntax error, unexpected 'as' , do i need to edit something?

        – justaguytrynabuildawebsite
        Jan 1 at 1:38






      • 1





        Should be foreach, not for

        – Nick
        Jan 1 at 1:39



















      • im getting a Parse error: syntax error, unexpected 'as' , do i need to edit something?

        – justaguytrynabuildawebsite
        Jan 1 at 1:38






      • 1





        Should be foreach, not for

        – Nick
        Jan 1 at 1:39

















      im getting a Parse error: syntax error, unexpected 'as' , do i need to edit something?

      – justaguytrynabuildawebsite
      Jan 1 at 1:38





      im getting a Parse error: syntax error, unexpected 'as' , do i need to edit something?

      – justaguytrynabuildawebsite
      Jan 1 at 1:38




      1




      1





      Should be foreach, not for

      – Nick
      Jan 1 at 1:39





      Should be foreach, not for

      – Nick
      Jan 1 at 1:39













      0














      use foreach for it.



      foreach ($list as $key => $value){

      }





      share|improve this answer




























        0














        use foreach for it.



        foreach ($list as $key => $value){

        }





        share|improve this answer


























          0












          0








          0







          use foreach for it.



          foreach ($list as $key => $value){

          }





          share|improve this answer













          use foreach for it.



          foreach ($list as $key => $value){

          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 5:20









          PHP GeekPHP Geek

          1,3861718




          1,3861718























              0














              I think this is what you want



              foreach($list as $key => $value ){  
              $arr = array_keys($value);//this has your (6, 8 or 10)
              foreach($arr as $val){
              print_r($value[$val]);//showing array data of 6,8,10 indexes
              }
              }


              output:
              enter image description here






              share|improve this answer




























                0














                I think this is what you want



                foreach($list as $key => $value ){  
                $arr = array_keys($value);//this has your (6, 8 or 10)
                foreach($arr as $val){
                print_r($value[$val]);//showing array data of 6,8,10 indexes
                }
                }


                output:
                enter image description here






                share|improve this answer


























                  0












                  0








                  0







                  I think this is what you want



                  foreach($list as $key => $value ){  
                  $arr = array_keys($value);//this has your (6, 8 or 10)
                  foreach($arr as $val){
                  print_r($value[$val]);//showing array data of 6,8,10 indexes
                  }
                  }


                  output:
                  enter image description here






                  share|improve this answer













                  I think this is what you want



                  foreach($list as $key => $value ){  
                  $arr = array_keys($value);//this has your (6, 8 or 10)
                  foreach($arr as $val){
                  print_r($value[$val]);//showing array data of 6,8,10 indexes
                  }
                  }


                  output:
                  enter image description here







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 6:21









                  KhageshKhagesh

                  1138




                  1138






























                      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%2f53992446%2fget-key-from-array-with-multiple-values%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