Arnas Pečelis
Arnas Pečelis

Reputation: 1008

Group items of array in categories

So I have multidimensional array with the following info:

    (array) category1
(array) category2
(array) category2 item
(array) category3
(array) category3 item
(array) category3 item
(array) category3 item
(array) category4
(array) category5
(array) category6
(array) category6 item
(array) category6 item

and I need that the reformated array would look like this:

array(
                        'category' => 'category1',
                        'items'    => array()
                    ),
                    array(
                        'category' => 'category2',
                        'items'    => array(
                            'item1'
                        ),
                    ),
                    array(
                        'category' => 'category3',
                        'items'    => array(
                            'item1', 'item2', 'item3'
                        )
                    )

and etc..

and everything I have no is worth a piece of big non sleep night:

foreach ( $fileData as $itemKey => $itemValue ) {
        if ( key($itemValue) == 'categoryData' ) {
            // define category

            $data['itemData'] = array(
                'categoryName' => $itemValue['categoryData']['category'],
                'subcat_1'     => $itemValue['categoryData']['subcat_1'],
                'subcat_2'     => $itemValue['categoryData']['subcat_2'],
                'subcat_3'     => $itemValue['categoryData']['subcat_3'],
                'items'        => array(),
            );

        } else {
            // category was defined, need to add items
            $data['itemData']['items'][] = $itemValue;
        }

        $data['returnData'][] = $data['itemData'];
    }

any ideas?

EDIT: parent array value:

    Array
(
    [0] => Array
        (
            [categoryData] => Array
                (
                    [category] => GAZ 
                    [subcat_1] => GAZELLE autobusas  (1993.10 - 2001.08)
                    [subcat_2] => 2.3 96 kW / 131 AG (1993.10 - 2001.08)
                    [subcat_3] => Apšvietimas
                )

        )

    [1] => Array
        (
            [categoryData] => Array
                (
                    [category] => GAZ 
                    [subcat_1] => GAZELLE autobusas  (1993.10 - 2001.08)
                    [subcat_2] => 2.3 96 kW / 131 AG (1993.10 - 2001.08)
                    [subcat_3] => Aušinimo sistema
                )

        )

    [2] => Array
        (
            [itemData] => Array
                (
                    [name] => Termostatas GATES TH43080
                    [photo] => ***
                    [price] => 13.90Eur
                    [description] => <p>pradine temperatura [°C] 80<br></p>
                )

        )

    [3] => Array
        (
            [categoryData] => Array
                (
                    [category] => GAZ 
                    [subcat_1] => GAZELLE autobusas  (1993.10 - 2001.08)
                    [subcat_2] => 2.3 96 kW / 131 AG (1993.10 - 2001.08)
                    [subcat_3] => Degalų tiekimo sistema
                )

        )

    [4] => Array
        (
            [categoryData] => Array
                (
                    [category] => GAZ 
                    [subcat_1] => GAZELLE autobusas  (1993.10 - 2001.08)
                    [subcat_2] => 2.3 96 kW / 131 AG (1993.10 - 2001.08)
                    [subcat_3] => Diržinė pavara
                )

        )
    )

Upvotes: 0

Views: 103

Answers (2)

Arnas Pečelis
Arnas Pečelis

Reputation: 1008

After a while I got result I wanted

$data = array(
            'returnData' => array(),
            'itemData'   => array()
        );
    $i = 0;

    foreach ( $fileData as $itemKey => $itemValue ) {
        if ( key($itemValue) == 'categoryData' ) {
            $i++;

            $data['itemData'][$i]['category'] = array(
                'categoryName' => $itemValue['categoryData']['category'],
                'subcat_1'     => $itemValue['categoryData']['subcat_1'],
                'subcat_2'     => $itemValue['categoryData']['subcat_2'],
                'subcat_3'     => $itemValue['categoryData']['subcat_3'],
            );
        } else {
            $data['itemData'][$i]['item'][] = array(
                'name' => $itemValue['itemData']['name'],
                'image' => $itemValue['itemData']['photo'],
                'price' => $itemValue['itemData']['price'],
                'description' => $itemValue['itemData']['description']
            );
        }
    }

    return $data['itemData'];

Upvotes: 1

bloodyKnuckles
bloodyKnuckles

Reputation: 12079

One problem is, with every element you're pushing a new element onto the $data['returnData] array, so you're not going to get the items nested under the categories.

Rather, keep track of the current category index $ii and push the items under it:

$ii = -1;
foreach ( $fileData as $itemValue ) {
    if ( key($itemValue) == 'categoryData' ) {
        // define category
        $data['returnData'][++$ii]['itemData'] = array(
            'categoryName' => $itemValue['categoryData']['category'],
            'subcat_1'     => $itemValue['categoryData']['subcat_1'],
            'subcat_2'     => $itemValue['categoryData']['subcat_2'],
            'subcat_3'     => $itemValue['categoryData']['subcat_3'],
            'items'        => array(),
        );
    } else {
        // category was defined, need to add items
        $data['returnData'][$ii]['itemData']['items'][] = $itemValue;
    }
}
print_r($data);

Notice when a categoryData element is hit the category index is pre-incremented: ++$ii. This means, first increment $ii then use it. So, since $ii is instantiated, before the loop, with "-1", the very first category gets the index "0".

Then all items that follow that category get pushed under that category's index.

When a new category comes long $ii is incremented, and a new category element is pushed onto $data['returnedData'] with that index.

Upvotes: 1

Related Questions