按类别对数组进行分组

时间:2015-04-03 21:21:07

标签: php arrays

所以我有多维数组,其中包含以下信息:

    (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

我需要重新格式化的数组看起来像这样:

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

等等。

我所拥有的一切都值得一个非睡眠之夜:

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'];
    }

任何想法?

编辑:父数组值:

    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
                )

        )
    )

2 个答案:

答案 0 :(得分:1)

一个问题是,每个元素都会将新元素推送到$data['returnData]数组,因此您不会将嵌套在类别

相反,跟踪当前类别索引$ii并推送其下的项目:

$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);

请注意,当点击 categoryData 元素时,类别索引预先递增: ++$ii。这意味着,首先增加$ii然后使用它。因此,自$ii实例化后,在循环之前,使用&#34; -1&#34; ,第一个类别获取索引 &#34; 0&#34;

然后,类别之后的所有都会被推送到该类别的索引下。

当一个新的类别到来时,$ii会递增,并且新的类别元素会被推送到$data['returnedData']的索引。

答案 1 :(得分:1)

过了一会儿我得到了我想要的结果

$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'];