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 7552813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:52:51+00:00 2026-05-30T10:52:51+00:00

For the sake of being able to search and index/sort an array I have

  • 0

For the sake of being able to search and index/sort an array

I have a large multidimensional array that I am trying to parse. Right now, I am simply using the CakePHP shell to do so, but any method will suffice. What I need to do, is take the array of parent/children/more children and create an array of id’s with the associated ‘Outline Number’

So for example:

1
1.1
1.2
1.2.1
1.2.2
1.2.3

And so forth.

Here is an example of my ‘Input Array’:

Array
(
    [0] => Array
        (
            [AsapStructure] => Array
                (
                    [id] => 1
                    [lft] => 30267
                    [rght] => 32774
                    [parent_id] => 
                    [wbs] => 
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [AsapStructure] => Array
                                (
                                    [id] => 2
                                    [lft] => 30268
                                    [rght] => 30773
                                    [parent_id] => 1
                                    [wbs] => 1
                                )

                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [AsapStructure] => Array
                                                (
                                                    [id] => 3
                                                    [lft] => 30269
                                                    [rght] => 30382
                                                    [parent_id] => 2
                                                    [wbs] => 1.1
                                                )

What I envisioned, was having a simple parsing mechanism that is self-recursive. An example of what I have developed is as follows:

<?php
    var $structIndex = array();

    private function __parseStruct( $toParse,$prefix = null ) {
        $iterator = 0;
        if ( $prefix )
            $prefix = $prefix . '.';

        foreach( $toParse as $datum ) {
            $iterator++;
            if ( $datum['AsapStructure']['id'] == 1 )
                $this->structIndex[ 1 ] = NULL;
            else            
                $this->structIndex[ $datum['AsapStructure']['id'] ] = $prefix . $iterator  ;

            $subiterator = 0;
            foreach( $datum['children'] as $key => $data ) {
                $subiterator++;
                $this->structIndex[ $data['AsapStructure']['id'] ] = $prefix . $subiterator;
                if ( ! empty( $datum['children'] ) ) {
                    $this->__parseStruct( $datum['children'],  $subiterator );
                }
            }
        }
        return $this->structIndex;
    }

?>

What I do, is I call this $this->__parseStruct( $data ) function and pass the array to it. The function then loops over the array, calling the function for the nested child arrays. I have that part working okay, I just can’t seem to get the logic correct for creating the ‘indexes’, that is the 1.1, 1.2, 1.2.1 and so forth.

So my target output would then be:

$array[ $rowid ] = [ 1.1 / 1.2 / 1.2.1 / 1.2.2 ] or any combination as such.

Any help is greatly appreciated.

  • 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-05-30T10:52:52+00:00Added an answer on May 30, 2026 at 10:52 am

    I was on the right track, I was just adding an extra unnecessary step.

    private function __parseStruct( $toParse,$prefix = null ) {
    
        $iterator = 0;
    
        if ( $prefix )
            $prefix = $prefix . '.';
    
        foreach( $toParse as $datum ) {
    
            $iterator++;
    
            if ( $datum['AsapStructure']['id'] == 1 )
                $this->structIndex[ 1 ] = NULL;
            else            
                $this->structIndex[ $datum['AsapStructure']['id'] ] = $prefix . $iterator  ;
    
    
    
            if ( ! empty( $datum['children'] ) ) {
    
                $this->__parseStruct( $datum['children'],  $prefix.$iterator );
    
            }
    
    
    
        }
    
        return $this->structIndex;
    
    }
    

    This solved the issue. However, due to the nature of my array, it was necessary for me to call the function initially by doing: $this->__parseStruct( $data[0]['children'] ); otherwise I ended up with a 1. prefixing every data element in the resulting array.

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

Sidebar

Related Questions

I have a PHP script that runs through an array of values through my
For brevity's sake in my code, i'd like to be able to do the
For simplicities sake, I'll make up a similar example to what I have: Let's
For the sake of convenience I am trying to assign multiple values to a
For my curiosity sake I'm looking for a dynamic object oriented language that allows
This question is just for a sake of knowledge. I don't have any practical
Say I have two tables, user and comment . They have table definitions that
Hey everyone I am building a website that needs to have the capability to
I have a form that allows a user to add an unlimited number of
I figured I would make this a different question for the sake of being

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.