1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

how can i get key from this code?

Discussion in 'Development' started by korado531m7, Mar 22, 2018.

  1. korado531m7

    korado531m7 Slime

    Messages:
    77
    GitHub:
    korado531m7
    There is a code
    Code:
    $data["red"] = array("abc","def","ghi");
    $data["blue"] = array("jul");
    $data["green"] = array("mno","pqr","stu");
    $data["yellow"] = array();
    
    how can i get "green" key using "pqr" ?

    i tried using array_search, but it doesn't work
    Code:
    echo array_seach(array("pqr"),$data);
    //Result: NONE
    
    working code is
    Code:
    foreach($data as $key => $a){
        foreach($a as $b){
            echo $key."-".$b."\n";
        }
    }
    
    but, i don't wanna use foreach (loop)

    please tell me. thank you for reading and answering.
     
    Last edited: Mar 22, 2018
  2. di2134876

    di2134876 Spider Jockey

    Messages:
    29
    GitHub:
    dk1234987
    I don't think you can get a value from a key without knowing the key.
    Maybe reverse the values
    Or try:
    PHP:
    for($i 0$i 3$i++){
        if(
    in_array("whateveruwanttocheck"$data[$i])){
            
    //your code
        
    }
    }
     
  3. korado531m7

    korado531m7 Slime

    Messages:
    77
    GitHub:
    korado531m7
    as i know the value and key.
    and return the key from value, how to?
     
  4. Kenn Fatt

    Kenn Fatt Slime

    Messages:
    82
    GitHub:
    kennfatt
    Here is my point:
    You are trying to get array key with known value.

    PHP:
    foreach($data as $key => $value) {
      if(
    $value == "pqr") {
        return 
    $key// Yea, you got that "green"
      
    }
    }
     
    korado531m7 likes this.
  5. korado531m7

    korado531m7 Slime

    Messages:
    77
    GitHub:
    korado531m7
    can i return the key without using foreach?
     
  6. di2134876

    di2134876 Spider Jockey

    Messages:
    29
    GitHub:
    dk1234987
    PHP:
    if(in_array("pqr"$data["green"])){
       
    }
    If you want the key even though you know it
     
  7. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    there's arraykey too
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    If you don't want to use a for/foreach loop, I think you should rewrite what you're doing in an efficient way. Structure your array in such a way that you won't require looping through it.
    You can use array_filter, though it's said a foreach loop would be much faster.
    PHP:
    $keys array_keys(array_filter(function(array $array) : bool{
        return 
    in_array("pqr"$array);
    }, 
    $data));
    var_dump($keys);
    Try benchmarking that with a foreach loop.
    PHP:
    foreach($data as $key => $array){
        if(
    array_search("pqr"$arraytrue) !== false){
            break;
        }
        
    $key null;
    }
    var_dump($key);
     
    Last edited: Mar 22, 2018
  9. korado531m7

    korado531m7 Slime

    Messages:
    77
    GitHub:
    korado531m7
    which is faster?
     
  10. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    One of them is, over the other.
     
  11. Kenn Fatt

    Kenn Fatt Slime

    Messages:
    82
    GitHub:
    kennfatt
    FYI:
    If you need which way is faster, here the explanation:
    First, foreach method has 0(n),
    Second, array_search also has 0(n).

    And I can said @musqit ways are slower than others. He used foreach and also array_search.
    As my experience (also I did some research), in_array was very worst if your array has big values.

    If you need faster way, you can use Binary Search method if known data are use-able with it.

    EDITED:
    This argument are only for non-array values.
     
    Last edited: Mar 24, 2018
  12. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You are kidding, right? Please tell me you are.
    All other methods by others don't work at all. Including yours!
     
    Last edited: Mar 24, 2018
  13. Kenn Fatt

    Kenn Fatt Slime

    Messages:
    82
    GitHub:
    kennfatt
    Ouchhh, I don't meant.
     
  14. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Your method doesn't work anyway, boo.
     
  15. Kenn Fatt

    Kenn Fatt Slime

    Messages:
    82
    GitHub:
    kennfatt
    I forgot the value was an array, yes you are the best.
     
  16. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    That isn't my point. You say my method is the slowest while your method doesn't even work. Id rather choose slow over not working at all.
    Plus you haven't given any examples to support your "binary search" methodology.
     
    Last edited: Mar 24, 2018
    LewBr and Kenn Fatt like this.
  17. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Create a flipped mapping from "pqr" to "green". Create a data structure class for it if it is too hard to maintain.
     
  18. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    PROGRAMMERS FIGHT, I've never seen this in my insignificant life.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.