V-jay
V-jay

Reputation: 79

Read the categories and subcategories in csv format and prepare multidimensional array

In the PHP, I need to convert the below image CSV file in this array format:

This code for creating categories and subcategories. If client will add the extra categories and subcategories its need to work as per that. So I need the dynamic one.

enter image description here

Array Format :

Array
(
    [0] => Cateory1
    [Cateory1] => Array
        (
            [0] => SubCategory11
            [1] => SubCategory12
            [2] => SubCategory13
            [3] => SubCategory14
        )

    [1] => Cateory2
    [Cateory2] => Array
        (
            [0] => SubCategory21
            [1] => SubCategory22
            [2] => SubCategory23
                [SubCategory23] => Array
                (
                    [0] => SubCategory221
                    [1] => SubCategory222
                )
            [3] => SubCategory24
        )

    [2] => Cateory3
    [Cateory3] => Array
        (
            [0] => SubCategory31
            [1] => SubCategory32
            [2] => SubCategory33
        )

    [3] => Cateory4
    [Cateory4] => Array
        (
            [0] => SubCategory41
            [SubCategory41] => Array
            (
                [0] => SubCategory411
                [SubCategory411] => Array
                (
                    [0] => SubCategory4111
                    [SubCategory4111] => Array
                    (
                        [0] => SubCategory41111
                    )
                )
            )
        )
)

Kindly help me to achieve this one.

Thanks.

Upvotes: 2

Views: 440

Answers (1)

Mathias Barresi
Mathias Barresi

Reputation: 36

Although i agree with Pitchinnate, I had some spare time and I hate parsing that sort of csv rubbish, so I thought I give it a try. Here is a piece of code that might get you going. I did not test or optimize anything, so if this doesn't work as expected, it might still point you in the right direction.

// I assume, $imput is a twodimensional array containing the csv data

// the multi dimensional result array
$result = array();

// accumulated categories of current-1 line
$lastCat = array();
foreach($input as $line) {
    // accumulated categories of current line
    $cat = array();



    //First we create an array in $cat that contains the full "breadcrumbs" per line e.g. 
    // ['category1', 'SubCategory11', 'SubCategory114', ...]
    $marker = PHP_INT_MAX;
    for ($i=0; $i<count($line); $i++) {
        if ($marker < $i) {
            $cat[$i] = '';
        } else if ($line[$i] != '') {
            $cat[$i] = $line[$i];
            $marker = $i;
        }
    }

    // then using these category breadcrumbs, we create the result arrays
    // you could do this using recursion if you want
    $tmp = $result;
    for ($i=0; $i<count($cat); $i++) {
        // if we haven't seen this category yet, create it
        // a bit bulky, but necessary as you want the categories with numeric indices as well
        if (!isset($lastCat[$i]) || $cat[$i] != $lastCat[]) {
            $tmp[] = $cat[$i];
            // Check if there are still subcategories left in this line...
            if (!empty($cat[$i+1])) {
                //... and if so, create the category named index if not already existing
                if (!array_key_exists($cat[$i], $tmp)) {
                    $tmp[$cat[$i]] = array();
                } 
                $tmp = $tmp[$cat[$i]];
            } else {
                // ... and if not, we are finished with this line
                break;
            }
        }
    }
    $lastCat = $cat;
}

Upvotes: 1

Related Questions