How can I get the "path" of a key in a multidimensional array?

peace_love

I have a multidimensional array:

$array = ["farm"=>
              [
                "horse"=>
                 [
                  "horseman"=>
                     [
                      "fred1"=>  "fred1",
                      "fred2"=> "fred2",
                      "fred3"=>"fred3",
                      "fred4"=>  "fred4"
                    ],
                 "horseman2"=>
                    ["john"=> "john"]
                  ]  
              ]
    ];  

And I am searching in this array for a specific key:

    function findKey($array, $keySearch) {
    foreach ($array as $key => $item) {
      $basePath = $basePath === null ? $key : $basePath. "/" . $key;
        if (stripos($key, $keySearch) !== false){
        echo "<li>Path:".$basePath."</li>"; 
            echo "<li>".$key."</li>";
         }   
        if (is_array($item))
           findKey($item, $keySearch); 
    }
}

findKey($array, 'horse');

What I need is simply the "paths" of my resulted keys.

But my result is:

  • Path:horse
  • horse
  • Path:horseman
  • horseman
  • https://eval.in/564230

    I tried so many ways, but I cannot achieve what I need :(


    Here some examples:

     findKey($array, 'horse');
    

  • Path:farm
  • horse
  • Path:farm/horse
  • horseman
  • Path:farm/horse
  • horseman2
  • findKey($array, 'horseman');
    

  • Path:farm/horse
  • horseman
  • Path:farm/horse
  • horseman2
  • findKey($array, 'horseman2');
    

  • Path:farm/horse
  • horseman2
  • Murad Hasan

    Try this function

    Check the Script online Script.

    Because you call the same function again and again, so you lost the value of basepath. So pass this every time you call the function.

    function findKey($array, $keySearch, $basePath) {
        $basePath2 = '';
        foreach ($array as $key => $item) {
            $basePath = ($basePath == "") ? $key : $basePath. "/" . $key;
            if($key == $keySearch){
                if(is_array($item)){
                    foreach($item as $key2 => $value){
                        $basePath2 = ($basePath2 == "") ? $key : $basePath2. "/" . $key;
                        echo "<li>Path:".$basePath."</li>"; 
                        echo "<li>".$key2."</li>";
                    }
                }           
                break;
            }else{
                if(is_array($item)){
                    foreach($item as $key3 => $value3){
                        echo "<li>Path:".$basePath."</li>";
                        echo "<li>".$key3."</li>";
                    }               
                }
            }
    
            if(is_array($item))
                findKey($item, $keySearch, $basePath);
        }
    }
    findKey($array, 'horse', '');
    

    Result

    • Path:farm
    • horse
    • Path:farm/horse
    • horseman
    • Path:farm/horse
    • horseman2

    Collected from the Internet

    Please contact [email protected] to delete if infringement.

    edited at
    0

    Comments

    0 comments
    Login to comment

    Related

    From Dev

    How can I get the "path" of a key in a multidimensional array?

    From Dev

    How can I get the child key of a key in a multidimensional array?

    From Dev

    How can I get the child key of a key in a multidimensional array?

    From Dev

    How can I get parent, grandparent and grandgrand parent keys of one array key in a multidimensional array?

    From Dev

    How can I find the key of the last occurrence of an item in a multidimensional array?

    From Dev

    How do i get the key name out of a multidimensional array

    From Dev

    How can I get the duplicate multidimensional array in php

    From Dev

    How can I get in_array() to work with multidimensional arrays?

    From Dev

    How can I find only keys from a multidimensional array that have a key as a child?

    From Dev

    How can I find the value of a certain key hidden somewhere in a multidimensional array?

    From Dev

    How can I find only keys from a multidimensional array that have a key as a child?

    From Dev

    How can I print a multidimensional array in Perl?

    From Dev

    How can I implode() a multidimensional array into a string?

    From Dev

    How can i sort this multidimensional array?

    From Dev

    How can I add a value to a multidimensional array?

    From Dev

    How to get part of array excluding one key in multidimensional array?

    From Dev

    PHP: Get key in multidimensional Array

    From Dev

    How can I get the key Values in a JSON array in ruby?

    From Dev

    How can I get the prev/next key from an associative array?

    From Dev

    PHP: How can I get the value from a key in a multiple array

    From Dev

    How can I recursively get the IDs of all the parent elements in a multidimensional array?

    From Dev

    How can I recursively get the IDs of all the parent elements in a multidimensional array?

    From Dev

    Multidimensional Array: How to get all values of a specific key?

    From Dev

    How to get this code to have a key value without creating a multidimensional array

    From Dev

    How to get id as key and email as its value from multidimensional array?

    From Dev

    How to get all values of one key from laravel multidimensional array

    From Dev

    How can I convert a multidimensional array into one level array?

    From Dev

    How can i remove an array from a multidimensional array in javascript?

    From Dev

    How can I replace a string inside a multidimensional array?

    Related Related

    1. 1

      How can I get the "path" of a key in a multidimensional array?

    2. 2

      How can I get the child key of a key in a multidimensional array?

    3. 3

      How can I get the child key of a key in a multidimensional array?

    4. 4

      How can I get parent, grandparent and grandgrand parent keys of one array key in a multidimensional array?

    5. 5

      How can I find the key of the last occurrence of an item in a multidimensional array?

    6. 6

      How do i get the key name out of a multidimensional array

    7. 7

      How can I get the duplicate multidimensional array in php

    8. 8

      How can I get in_array() to work with multidimensional arrays?

    9. 9

      How can I find only keys from a multidimensional array that have a key as a child?

    10. 10

      How can I find the value of a certain key hidden somewhere in a multidimensional array?

    11. 11

      How can I find only keys from a multidimensional array that have a key as a child?

    12. 12

      How can I print a multidimensional array in Perl?

    13. 13

      How can I implode() a multidimensional array into a string?

    14. 14

      How can i sort this multidimensional array?

    15. 15

      How can I add a value to a multidimensional array?

    16. 16

      How to get part of array excluding one key in multidimensional array?

    17. 17

      PHP: Get key in multidimensional Array

    18. 18

      How can I get the key Values in a JSON array in ruby?

    19. 19

      How can I get the prev/next key from an associative array?

    20. 20

      PHP: How can I get the value from a key in a multiple array

    21. 21

      How can I recursively get the IDs of all the parent elements in a multidimensional array?

    22. 22

      How can I recursively get the IDs of all the parent elements in a multidimensional array?

    23. 23

      Multidimensional Array: How to get all values of a specific key?

    24. 24

      How to get this code to have a key value without creating a multidimensional array

    25. 25

      How to get id as key and email as its value from multidimensional array?

    26. 26

      How to get all values of one key from laravel multidimensional array

    27. 27

      How can I convert a multidimensional array into one level array?

    28. 28

      How can i remove an array from a multidimensional array in javascript?

    29. 29

      How can I replace a string inside a multidimensional array?

    HotTag

    Archive