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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:39:08+00:00 2026-05-25T17:39:08+00:00

I need help to generate a tree of categories from a CSV file. I

  • 0

I need help to generate a tree of categories from a CSV file.

I tried many things, but I get lost …

I have a file of 800 lines that looks like this:

"LOADERS", "S130", "Above & 524611001, 524711001 & Above", "MAINTENANCE ITEMS", "SCHEDULED MAINTENANCE ITEMS (500 HOUR)"
"LOADERS", "S130", "Above & 524611001, 524711001 & Above", "MAINTENANCE ITEMS", "SCHEDULED MAINTENANCE ITEMS (1000 HOUR)"
"LOADERS", "S130", "Above & 524611001, 524711001 & Above", "MAIN FRAME", "OPERATOR CAB"

On those lines,
Column 1 represents the parent class
Column 2 is the child of Column 1
Column 3 is the child of Column 2
Column 3 is the mother of Column 4 (but it’s an explode (‘,’, Column 3))
Explode in column 4 are the mothers of 5

I would like to generate an array of this type (and created an ID starting at 50) because the goal is to convert this file into a mysql DB: s

I would succeed in having something like this

LOADERS (id = 50)
|___S130 (id = 51)
|----|____Above & 524611001 (id = 52)
|-----------|____ MAINTENANCE ITEMS (id = 53)
|------------------|____SCHEDULED MAINTENANCE ITEMS (500 HOUR) (id = 54)
|------------------|____SCHEDULED MAINTENANCE ITEMS (1000 HOUR) (id = 55)
|-----------|____ MAIN FRAME (id = 56)
|------------------|____OPERATOR CAB (id = 57)
|----|____Above & 524711001 (id = 58)
|-----------|____ MAINTENANCE ITEMS (id = 59)
|------------------|____SCHEDULED MAINTENANCE ITEMS (500 HOUR) (id = 60)
|------------------|____SCHEDULED MAINTENANCE ITEMS (1000 HOUR) (id = 61)
|-----------|____ MAIN FRAME (id = 62)
|------------------|____OPERATOR CAB (id = 63)

I tried a bunch of method and I begin to despair. Everything I did not even approaching the desired result: s I sweat on it since Monday and I want to hang myself.

  • 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-25T17:39:09+00:00Added an answer on May 25, 2026 at 5:39 pm

    Try something like this:

    <?php
    // generate categories tree with csv file
    // http://stackoverflow.com/questions/7498119/generate-categories-tree-with-csv-file
    
    $big_array  = array();
    $temp_array = array();
    
    $csv = <<< CSV
    "LOADERS", "S130", "Above & 524611001, 524711001 & Above", "MAINTENANCE ITEMS", "SCHEDULED MAINTENANCE ITEMS (500 HOUR)"
    "LOADERS", "S130", "Above & 524611001, 524711001 & Above", "MAINTENANCE ITEMS", "SCHEDULED MAINTENANCE ITEMS (1000 HOUR)"
    "LOADERS", "S130", "Above & 524611001, 524711001 & Above", "MAIN FRAME", "OPERATOR CAB"
    CSV;
    
    foreach(preg_split("/(\r?\n)/", $csv) as $line) {
    
        list($column1, $column2, $column3, $column4) = explode('", "', $line);
    
        $column1 = preg_replace('/^"/', '', $column1);
        $column4  = preg_replace('/\"(?<!\\")/', '', $column4);
    
        foreach(explode(', ', $column3) as $column3_part) {
            $temp_array[$column3_part][] = $column4;
        }
    
    
        $big_array[$column1][$column2] = $temp_array;
    }
    
    echo '<pre>' . print_r($big_array, 1) . '</pre><hr />';
    
    $counter = 50;
    foreach($big_array as $key1 => $level1) {
        foreach($level1 as $key2 => $level2) {
            foreach($level2 as $key3 => $level4) {
                foreach($level4 as $level5) {
    
                    echo '<pre>' . print_r('INSERT INTO DATABASE: ' . $counter . ', ' . $key1 . ', ' . $key2 . ', ' . $key3 . ', ' . $level5, 1) . '</pre>';
                    $counter++;
                }
            }
        }
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to help to generate column name from excel automatically. I think that:
I need help in this matter: We have a template of Excel file in
Need help writing a script downloads data from google insight using c# this is
i need help with disk_total_space function.. i have this on my code <?php $sql=select
I need help using Bouncy Castle RSA Libraries for a school assignment, but it
i'm using PDFizer library for .NET from here - PDFizer and i need help...
Need help with following situation: Users can generate their own data structures which are
I need help on how to validate Xml file simply? I googled and found
I need help writing a T-SQL query that will generate 52 rows of data
I need help for generate the series data part into json var chart =

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.