Home php Get key by array value

Get key by array value

Author

Date

Category

An array is available:

["pid"] = & gt;
 array (75) {
  [1] = & gt;
  string (1) "0"
  [2] = & gt;
  string (1) "0"
  [3] = & gt;
  string (1) "0"
  [4] = & gt;
  string (1) "0"
  [5] = & gt;
  string (1) "0"
  [6] = & gt;
  string (1) "0"
  [7] = & gt;
  string (1) "6"
  [8] = & gt;
  string (1) "6"
  [9] = & gt;
  string (1) "6"
  [10] = & gt;
  string (1) "6"
  [11] = & gt;
  string (1) "6"

and a loop like:

foreach ($ categories ['id'] as $ value = & gt; $ key) {
  // Let's say $ value = 6, then you need to pull the values โ€‹โ€‹of all keys from the pid array, where the element value is 6. That is, these will be keys 7,8,9,10,11.
}

In simple words, you need to find its ancestors (children) for an element with id


Answer 1, authority 100%

Just read docs … array_search ()

$ arr = array ('first' = & gt; 'a', 'second' = & gt; 'b' ,);
$ key = array_search ('a', $ arr);

and array_keys ()

$ array = array ("blue", "red", "green", "blue", "blue" );
print_r (array_keys ($ array, "blue"));

search_value
If specified, only keys containing the given value will be returned.


Answer 2, authority 20%

& lt ;? if (isset ($ categories_doctors)) {
        $ arr = array ();
        foreach ($ categories_doctors ['pid'] as $ val = & gt; $ key) {
          if (! empty ($ key)) {
            $ arr [$ key] [] = $ val;
          }
        }
        foreach ($ categories_doctors ['id'] as $ value = & gt; $ key) {
          if ($ categories_doctors ['pid'] [$ key] == 0) {
          echo '& lt; li & gt;';
          echo $ categories_doctors ['name'] [$ key];
            if (isset ($ arr [$ key])) {
              echo '& lt; ul & gt;';
              foreach ($ arr as $ key = & gt; $ val) {
                foreach ($ val as $ c) {
                  echo '& lt; li & gt;'. $ categories_doctors ['name'] [$ c]. '& lt; / li & gt;';
                }
              }
              echo '& lt; / ul & gt;';
              unset ($ arr [$ key]);
              unset ($ categories_doctors ['id'] [$ key]);
          }
          echo '& lt; / li & gt;';
        }
      }
    }
 ? & gt;

The $ arr array looks like this:

array (4) {
 [6] = & gt;
 array (7) {
  [0] = & gt;
  int (7)
  [1] = & gt;
  int (8)
  [2] = & gt;
  int (9)
  [3] = & gt;
  int (10)
  [4] = & gt;
  int (11)
  [5] = & gt;
  int (12)
  [6] = & gt;
  int (13)
 }
 [17] = & gt;
 array (1) {
  [0] = & gt;
  int (18)
 }
 [41] = & gt;
 array (6) {
  [0] = & gt;
  int (42)
  [1] = & gt;
  int (43)
  [2] = & gt;
  int (44)
  [3] = & gt;
  int (45)
  [4] = & gt;
  int (46)
  [5] = & gt;
  int (47)
 }
 [48] โ€‹โ€‹= & gt;
 array (2) {
  [0] = & gt;
  int (49)
  [1] = & gt;
  int (50)
 }
}

As I scroll through $ categories_doctors ['id'] , I turn to $ arr to find all descendants of the current node. if there is such a key, then I start the second cycle to display these descendants. The problem seems to be solved – but there is one thing – the same elements are output from the main array echo $ categories_doctors ['name'] [$ key];

As a result, I want to get the following construction:
& lt; ul & gt;
& lt; li & gt; Surgeon & lt; ul & gt; & lt; li & gt; Plastic surgeon & lt; / li & gt; & lt; li & gt; Simple surgeon & lt; / li & gt; & lt; / ul & gt; & lt; / li & gt;
& lt; li & gt; Speech therapist & lt; / li & gt;
& lt; / ul & gt;


Answer 3

In short there is an assumption only foreach twist.

/ **
 * @param $ Array Array Where we are looking for
 * @param $ item int what are looking for
 * /
Function My_Search ($ Array, $ Item) {
   $ result = array ();
   foreach ($ array as $ k = & gt; $ v) {
     if ($ v == $ item) {
       $ result [] = $ k;
     }
   }
   Return $ Result;
}

Well and use

my_search ($ Categories ['ID], 6);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions