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

  • Home
  • SEARCH
  • 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 8976637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:11:37+00:00 2026-06-15T19:11:37+00:00

I have a script that goes through a CSV file and puts each row

  • 0

I have a script that goes through a CSV file and puts each row as an array into another array (nested array?). Each row has 2-3 fields for the category of the item in that row. I’m trying to work through how to create a multidimensional array out of these categories. Here is the source I currently have:

$csv = new File_CSV_DataSource;
if ($csv->load($file)) {
    $items = $csv->getHeaders();
    $csv->getColumn($items[2]);
    if ($csv->isSymmetric()) {
        $items = $csv->connect();
    } else {
        $items = $csv->getAsymmetricRows();
    }
    $items = $csv->getrawArray();
}

$mainCats = array();
$subCats = array();
$subSubs = array();

foreach($items as $item){
    if(!in_array($item[10], $mainCats)){
        $mainCats[] = $item[10];
    }
}

foreach($items as $item){
    if(!array_key_exists($item[11], $subCats)){
        $parent = array_search($item[10], $mainCats);
        $subCats[$item[11]] = $parent;
    }
}

foreach($items as $item){
    if(!array_key_exists($item[12], $subSubs)){
        $parent = array_search($item[11], array_keys($subCats));
        $subSubs[$item[12]] = $parent;
    }
}

What this does so far is create 3 arrays with the format of:

$mainCats = Array(
    [0] => Main Cat 1,
    [1] => Main Cat 2,
    [2] => Main Cat 3
);

$subCats = Array(
    [Sub Cat 1] => 0,
    [Sub Cat 2] => 1,
    [Sub Cat 3] => 2
);

$subSubs = Array(
    [Sub Sub 1] => 0,
    [Sub Sub 2] => 1,
    [Sub Sub 3] => 2
);

The numeric values of each of the last 2 arrays are the index of their parent category in the previous array. What I would like to do is to merge them all into one large array in the format of:

$cats = Array(
    [0] => Array(
        'name' => Main Cat 1,
        'subs' => Array(
            [0] => Array(
                'name' => Sub Cat 1,
                'subs' => Array(
                    'name' => Sub Sub 1
                )
            )
        )
    ),
    [1] => Array(
        'name' => Main Cat 2,
        'subs' => Array(
            [0] => Array(
                'name' => Sub Cat 2,
                'subs' => Array(
                    'name' => Sub Sub 2
                )
            )
        )
    ),
    [2] => Array(
        'name' => Main Cat 3,
        'subs' => Array(
            [0] => Array(
                'name' => Sub Cat 3,
                'subs' => Array(
                    'name' => Sub Sub 3
                )
            )
        )
    ),
);

I know there has to be a far more efficient way of doing this, but I can’t figure it out.

EDIT – I should also mention that not all rows have a 3rd category field value.

  • 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-15T19:11:38+00:00Added an answer on June 15, 2026 at 7:11 pm

    I prefer to index them by name:

    $cats=array();
    //--------
    foreach($items as $item){
            $main=$item[10];
            $subCat=$item[11];
            $subSub[$item[12]];
            $cats[$main]['subs'][$subCat]['subsubs'][$subSub]['name']=$subSub;
            $cats[$main]['subs'][$subCat]['name']=$subCat;
            $cats[$main]['name']=$main;
    
    
    
    }
    
    • 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 imports CSV files and goes through tens of
Suppose I have a bash script that goes through a file that contains a
I have a PHP script that goes through an XML file, but I want
I have a script that has PHP on it, and a link that goes
I have a for loop that goes through each input with class=required , and
I am trying to write a bash script that goes through a file line
I have a script that is looping through an array of orders with an
I have pretty simple problem. I have a large file that goes through three
I have written a script that goes through a bunch of files and snips
I have part of my script that goes like this: while read line do

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.