SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best way to do array_search on multi-dimensional array?

    Best way to do array_search on multi-dimensional array?

    $array['key1']['key2']['key3'] = "hello";

    So, somehow, I provide "hello" and it returns array('key1', 'key2', 'key3'); - that would be ideal.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    There are a lot of user comments in the PHP docs that can help you with questions such as this, see the below link.

    http://www.php.net/manual/en/functio...rch.php#106904

  3. #3
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but the link you posted to does the opposite of what I want.

    I can't find anything on there that does what I want it to, they all seem to be the same way as the link you posted.

    The link would allow providing 'key1', 'key2', 'key3' and returning 'value'
    Code:
    $array['key1']['key2']['key3'] = "value";
    ...but I want to provide 'value' and return key1, key2, key3 etc.

    Thanks for posting though.

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Have a look through the docs page as i went down the page and found quite a few more comments with some handy functions that might help. Also i doubt you would be able to find an exact code source for what your wanting as retrieving the index for a specific search is faster then storing each key index.

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
    function array_find_deep($array$search$keys = array())
    {
        foreach(
    $array as $key => $value) {
            if (
    is_array($value)) {
                
    $sub array_find_deep($value$searcharray_merge($keys, array($key)));
                if (
    count($sub)) {
                    return 
    $sub;
                }
            } elseif (
    $value === $search) {
                return 
    array_merge($keys, array($key));
            }
        }

        return array();
    }

    $a = array(
        
    'key1' => array(
            
    'key2' => array(
                
    'key3' => 'value',
                
    'key4' => array(
                    
    'key5' => 'value2'
                
    )
            )
        )
    );

    var_dump(array_find_deep($a'value'));
    /*
    array
      0 => string 'key1' (length=4)
      1 => string 'key2' (length=4)
      2 => string 'key3' (length=4)
    */

    var_dump(array_find_deep($a'value2'));
    /*
    array
      0 => string 'key1' (length=4)
      1 => string 'key2' (length=4)
      2 => string 'key4' (length=4)
      3 => string 'key5' (length=4)
    */

    var_dump(array_find_deep($a'value3'));
    /*
    array
      empty
    */ 
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •