1
array(2) { [0]=> array(2) { ["name"]=> string(16) "Daerah Pertanian" ["sub"]=> array(6) { [0]=> array(2) { ["name"]=> string(5) "Sawah" ["value"]=> string(3) "145" } [1]=> array(2) { ["name"]=> string(18) "Sawah Pasang Surut" ["value"]=> string(3) "455" } [2]=> array(2) { ["name"]=> string(6) "Ladang" ["value"]=> string(3) "678" } [3]=> array(2) { ["name"]=> string(10) "Perkebunan" ["value"]=> string(3) "688" } [4]=> array(2) { ["name"]=> string(19) "Perkebunan Campuran" ["value"]=> string(3) "966" } [5]=> array(2) { ["name"]=> string(16) "Tanaman Campuran" ["value"]=> string(3) "565" } } } [1]=> array(2) { ["name"]=> string(22) "Daerah Bukan Pertanian" ["sub"]=> array(2) { [0]=> array(2) { ["name"]=> string(18) "Hutan Lahan Kering" ["sub"]=> array(2) { [0]=> array(2) { ["name"]=> string(25) "Hutan Lahan Kering Primer" ["value"]=> string(3) "566" } [1]=> array(2) { ["name"]=> string(27) "Hutan Lahan Kering Sekunder" ["value"]=> string(3) "255" } } } [1]=> array(2) { ["name"]=> string(17) "Hutan Lahan Basah" ["sub"]=> array(2) { [0]=> array(1) { ["name"]=> string(24) "Hutan Lahan Basah Primer" } [1]=> array(1) { ["name"]=> string(26) "Hutan Lahan Basah Sekunder" } } } } } }

I have an array like I mention above, so I want to print out every "name" key including the index (number) of it's array parent,

for example when I print out "Tanaman Campuran" so all index parent is (0)(5) and when I print "Hutan Lahan Basah Sekunder" the index parent is (1)(1)(1)

how can I achieve it?

here is some recursive function that I've tried

$GLOBALS['all'] = '';
    function printout($arr){
        foreach ($arr as $ia=>$a){
            if(is_array($a)){
                foreach ($a as $ib=>$b){
                    if(is_array($b)){
                        printout($b);
                    }
                    else{
                        if ($ib == 'name') {
                            $GLOBALS['all'] .= $ia;
                            echo '<tr>';
                            echo '<td>' . $b . ' (' . $ia . ')</td>';
                            echo '</tr>';
                            $GLOBALS['all'] = '';
                        }
                    }
                }
            }
        }
    }

*sorry for my bad explanation, I hope you guys can understand it

2
  • post you try first otherwise no one will help you out Commented Dec 8, 2018 at 13:12
  • I've edit my question, and adding my sample code, please checkout Commented Dec 8, 2018 at 13:17

1 Answer 1

1

You could use the following function:

function search(array $array, $name)
{
  foreach ($array as $key => $entry) {
    if ($entry['name'] === $name) {
      return [$key];
    }
    if (isset($entry['sub']) && $found_keys = search($entry['sub'], $name)) {
      return array_merge([$key], $found_keys);
    }
  }
  return null;
}

It returns:

  • if the value was directly found, an array of one containing the associated index,
  • if it wasn't but was found in any descendant item, an array merging its index with the indices of said descendant,
  • null if it wasn't found in that part of the tree.

Note: if a given name is present several times, it will only find the first occurrence.

Demo: https://3v4l.org/1hGr1

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.