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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:20:15+00:00 2026-05-20T00:20:15+00:00

I have a script which handles the naming of parent/child elements on another page.

  • 0

I have a script which handles the naming of parent/child elements on another page. The format for the name is like E5-2-3 which represents the third child of the second child of the fifth element.

What I need to do is pass in the parent name to the function and return the name for the next child. This value would be the increment of the last child or 1 if it is the first child.

(I hope this makes some sense to someone)

The index array looks something like this:

1=>null
2=>null
3=>
    1=>null
    2=>null
    3=>
        1=>null
4=>null
5=>
    1=>null
    2=>
        1=>null
        2=>null
        3=>null //the element I was talking about above
6=>
    1=>null
7=>null

My Code so far is

    $projectNumber = $_GET['project_number'];
    @$parentNumber = $_GET['parent_number']; //suppressed as it may not be set

    $query = mysql_query("SELECT e_numbers FROM project_management WHERE project_number = '$projectNumber'");
    $resultArray = mysql_fetch_assoc($query);
    $eNumbers = unserialize($resultArray['e_numbers']);

    if (!is_array($eNumbers)&&!isset($parentNumber)){ //first e_number assigned
        $eNumbers[1] = null; //cant possibly have children so null for now
        $nextENumber =  'E1';
    }else{
        if (!isset($parentNumber)){
            $nextNumber = count($eNumbers)+1;
            $eNumbers[$nextNumber] = null; //cant possibly have children so null for now
            $nextENumber = 'E'.$nextNumber;
        }else{
            $parentIndex = explode('-', str_replace('E', '', $parentNumber));
            //$nextENumber = //assign $nextENumber the incremented e number
        }
    }

    echo $nextENumber;

            //(then goes on to update sql etc etc)

This is all fine but for the line where I need to get/assign deep numbers. I think this should be some kind of recursive function based on the $parentIndex and $eNumbers arrays, however I’m a bit out of my depth when it comes to recursion.

Any pointer in the right direction will be a great help.

PS
If there is a better way to handle incrementing parent/child relationships I’m all ears. The only thing out of my control is the format of the numbers being passed in/out (Has to be EX-Y-Z-...)

UPDATE I was able to develop @ircmaxell ‘s function to function more better in my context. The function required you to pass in a zero based array(can be empty) and an optional path. It returns the new path and updates the index array to include the new path. An error message is returned if the index is not found.

function getNextPath(&$array, $path) { //thanks to  ircmaxell @ stackoverflow for the basis of this function
            $newPath = '';
            $tmp =& $array;
            if (is_string($path)) {
                $path = explode('-', str_replace('E', '', $path));
                $max = count($path);            
                foreach ($path as $key => $subpath) {
                    if (is_array($tmp)) {
                        if (array_key_exists($subpath, $tmp)){
                            $tmp =& $tmp[$subpath];
                                $newPath[] = $subpath;
                        }else{
                            return "Parent Path Not Found";
                        }

                    }
                }
            }           
            $tmp[] = null;
            $newPath[] = count($tmp)-1;
            if (count($newPath)>1){
                $newPath = implode('-', $newPath);
            }else{
                $newPath = $newPath[0];
            }           
            return "E".$newPath;
        }
  • 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-20T00:20:15+00:00Added an answer on May 20, 2026 at 12:20 am

    Here’s one way:

    function incrementPath(&$array, $path) {
        if (is_string($path)) {
            $path = explode('-', str_replace('E', '', $path);
        }
        $tmp =& $array;
        foreach ($path as $subpath) {
            if (is_array($tmp) && isset($tmp[$subpath])) {
                $tmp =& $tmp[$subpath];
            } else {
                return false; // Could not find entire path
            }
        }
        $tmp++;
        return true;
    }
    

    Now, if you want it to dynamically create paths, just change the return false; to:

    $tmp[$subpath] = array();
    $tmp =& $tmp[$subpath];
    

    And then add a check after the loop to see if it’s not an integer, and explicitly set to 0 if it isn’t…

    Edit: AHHH, now I understand:

    function getNextPath(&$array, $path) {
        if (is_string($path)) {
            $path = explode('-', str_replace('E', '', $path);
        }
        $newPath = '';
        $tmp =& $array;
        $max = count($path) - 1;
        foreach ($path as $key => $subpath) {
            if (is_array($tmp) && isset($tmp[$subpath])) {
                $tmp =& $tmp[$subpath];
                if ($key < $max) {
                    $newPath .= '-'.$subpath;
                }
            } else {
                return 'E' . ltrim($newPath . '-1', '-'); // Could not find entire path
            }
        }
        if (is_array($tmp)) {
            return 'E' . ltrim($newPath . '-' . count($tmp), '-');
        } else {
            //it's a value, so make it an array
            $tmp = array();
            return 'E' . ltrim($newPath . '-' . 1, '-');
        }
    }
    

    I think that should do what you want (it returns the next available path under what you’re looking for).
    }

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

Sidebar

Related Questions

I have the following script on this page which handles the fading in and
I have a script which hides (display:none) certain divs in the list on page
I have a script which creates a user-defined object like this: obj = new
I have a javascript script like this below which the 'args' contain a string
Currently I have a JQuery script in place http://jsfiddle.net/gaby/zzj7E/5/ which handles the requirement that
I have script which allows to display Bing search results. I can call for
I have a script which contacts a few sources and tell them the IP-address
I have a script which logs on to a remote server and tries to
I have a script which will be run interactively by non-technical users. The script
I have this script which basically toggles a bgColor class on and off so

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.