Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7894945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:31:22+00:00 2026-06-03T07:31:22+00:00

This is my array: $array = array ( ‘1’ => array( ‘title’ => ‘Level1’,

  • 0

This is my array:

$array = array (
'1' => array(
  'title' => 'Level1',
  'nodes' => array(
   '11' => array('title' => 'sub1_company1'),
   '12' => array('title' => 'sub2_company1'),
   '13' => array(
     'title' => 'sub3_company1',
     'nodes' => array(
       '131' => array('title' => 'item1_sub3_company1'),
       '132' => array('title' => 'item2_sub3_company1'),
      ),
    ),
  ),
),

'2' => array(
  'title' => 'Level2',
  'nodes' => array(
   '21' => array('title' => 'sub1_company2'),
   '22' => array('title' => 'sub2_company2'),
  ),
),

'3' => array(
  'title' => 'Level3',
  'nodes' => array(
   '31' => array('title' => 'sub1_company3'),
   '32' => array(
     'title' => 'sub2_company3',
     'nodes' => array(
       '321' => array('title' => 'item1_sub2_company3'),
       '322' => array(
          'title' => 'item2_sub2_company3',
          'nodes' => array(
            '3221' => array('title' => 'item1_sub3_company3'),
          ),
        ),
      ),
    ),
  ),
),
'4' => array('title' => 'Level4'),);

What I need is to find sub2_company1 and get the titles like a breadcrumb.

Level1 > sub2_company1

or if I search for item1_sub3_company1 I will get

Level1 > sub3_company1 > item1_sub3_company1

What I did so far but isn’t working:

    function breadcrumb($array, $needle) {
  $path = array();

  $array_iterator = new recursiveArrayIterator($array);
  $it = new recursiveIteratorIterator($array_iterator, RecursiveIteratorIterator::SELF_FIRST);

  foreach($it as $key => $value) 
  {
    echo "$key: $value <br>";
    if (!is_array($value)) {
      array_push($path, $value);
    }
    if ($value === $needle) {
      break;
    }
  }

  //$content = '<div id="breadcrumb">' . implode('&nbsp;&#62;&nbsp;', $path) . '</div>';

  return print_r($path, 1);
}

thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:31:23+00:00Added an answer on June 3, 2026 at 7:31 am

    You need a recursive function, not an iterative one.

    function breadcrumb($tree, $needle, &$result = array()) {
    
        $result = array();
    
        if (is_array($tree)) {
            foreach ($tree as $node) {
                if ($node['title'] == $needle) {
                    $result[] = $node['title'];
                    echo '1-';
                    return true;
                } else if (!empty($node['nodes'])) {
                    if (breadcrumb($node['nodes'], $needle, $result)){
                    echo '2-';
                    $result[] = $node['title'];
                    return true;
                    }
                }
            }
        } else {
            if ($tree == $needle) {
                echo '3-';
                $result[] = $tree;
                return true;
            }
        }
        return false;
    }
    
    breadcrumb($array, 'item1_sub3_company3', $result);
    
    print_r($result);
    

    The breadcrumb is inverted but you could use array_shift instead of push and you would have it the right way…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this array string[] presidents = { Adams, Arthur, Buchanan, Bush, Carter, Cleveland, Clinton,
My Array looks like this: Array ( [Team] => Array ( [0] => Array
Given an array like this: Array => ( [0] => 1, [1] => 2,
Array starts like this: Array ( [SMART Board] => Array ( [0] => sb1
Array is like this: Array ( [0] => 2011/10/05 ) Array ( [0] =>
I have this array in PHP: array( [0] => array( 'username' => 'user1' )
how would you convert this array: Array ( [0] => Array ( [Contact] =>
Trying to sort this array of objects according to (1) depth and (2) weight,
i want to convert this array Array ( [2] => Array ( ) [4]
I have this array, for example (the size is variable): x = [1.111, 1.122,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.