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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:03:02+00:00 2026-06-13T04:03:02+00:00

My intent is to take a series of strings, explode them by _ and

  • 0

My intent is to take a series of strings, explode them by “_” and use the keys from that array to build a “master” array.

My rules are:

if key no exists -> create key under parent array
if key exists -> add key as new array to parent
if key is last -> add value

String Examples

total_players_count
total_rosters_count
players_season1_count
players_season2_count
rosters_season1_count

Expected Results

$main = array(
    'total' => array(
        'players'   => array(
            'count' => '123',
        'rosters'   => array(
            'count' => '123')
        )
    ),
    'players' => array(
        'season1'   => array(
            'count' => '123'
        )
    )
);

I hope this shows what I’ve expected to write. Now let me lead into how I disappointed myself through a series of failures last night 😉

The gist is, my recursive functions haven’t been working so well so I’m not posting any of that code. I’ve been working with array_key_exists and that isn’t getting me my expected results. I came close using prev(), current(), and end() but the issue stems from an unexpected number of keys I have to parse through (otherwise I would just loop 3 times and be done. I know the following would work if I had a limited amount of keys to parse; but I don’t.

<?php
private function _get_section($parent_key, $sql) {

        $data = array();
        $data[$parent_key] = array();

        foreach ($sql AS $key => $value) {
            $keys       = explode('_', $key);

            if ($keys[0] == $parent_key) {
                $i          = 0;
                $total_keys = count($keys);

                for ($k = 1; $k < $total_keys; $k++) {
                    $i++;

                    if ($i == 1) {
                        echo '1. (' . $i . ') ' . $keys[$k];
                        $data[$parent_key][$keys[$k]] = array();
                    }
                    else if ($i > 1 && $i < $total_keys - 1) {
                        $data[$parent_key][$keys[$k - 1]][$keys[$k]] = array();
                    }
                    else if ($i == $total_keys - 1) {
                        $tmp = array_reverse($data);

                        // can't get the last key because I need to recursively loop 
                        // through the results to find where to set this last 
                        // key / value
                    }
                }
                $k      = 0;
                $i      = 0;
            }

            $keys       = array();
            $total_keys = 0;
        }
    }

Solution (thank you)

$tree = array();

foreach ($sql AS $key => $value) {
    $parts = explode('_', $key);

    $val = $value;

    foreach (array_reverse($parts) AS $part) {
        $val = array($part => $val);
    }
    $tree = array_merge_recursive($tree, $val);
}
  • 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-13T04:03:04+00:00Added an answer on June 13, 2026 at 4:03 am

    untested. hopefully i didnt need to read your code to fully understand what you want, because i didnt. i just looked at the expected results + the input, although i wonder what happened to rosters_season1_count?

    $tree = array();
    foreach ($strings as $string) {
        $parts = explode('_', $string);
        $path = '123';
        foreach (array_reverse($parts) as $part) {
            $path = array($part => $path);
        }
        $tree = array_merge_recursive($tree, $path);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use a standard Intent that will take a picture, then allow
I'm trying to take a picture from the Camera using intent with onActivityResult but
I have this code: startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), CAMERA_IMAGE); That allows that user to take a
I use the Android camera to take a picture in my activity : Intent
Trying to take an Array position(toString) and pass that to a new activity, then
I have a program that opens the camera via Intent to take a photo.
In one of our applications we use the camera intent to take pictures. This
I use following code take a Picture from camera and to obtain picture's path.
I am writing an app that uses the camera intent to take a picture.
I am trying to take a picture using an intent. My problem is that

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.