如何在php中动态拆分数组

时间:2016-12-14 16:54:08

标签: php

先谢谢,实际上我有一个包含元素"BUSINESS_NAME" => FOOD AND GROCERY and "BUSINESS_NAME" => NONFOOD的数组,所以我必须根据"FOOD AND GROCERY""NONFOOD"分隔数组元素 我的代码如下:

<?php
$arrFirst = Array
(
    "0" => Array
        (
            "BUSINESS_NAME" => FOOD AND GROCERY,
            "Division_name" => BAKERY AND CONCEPTS,
            "value" => 0.0009477694110518376
        ),

    "1" => Array
        (
            "BUSINESS_NAME" => FOOD AND GROCERY,
            "Division_name" => DAIRY AND FROZEN,
            "value" => 0.005450625689261974
        ),

    "2" => Array
        (
            "BUSINESS_NAME" => FOOD AND GROCERY,
            "Division_name" => FISH AND MEAT,
            "value" => 0.000021331155586759062
        ),

  "3" => Array
        (
            "BUSINESS_NAME" => FOOD AND GROCERY,
            "Division_name" => FOODS,
           "value" => 7.763433792429072 
        ),

    "4" => Array
        (
            "BUSINESS_NAME" => FOOD AND GROCERY,
            "Division_name" => FMCGFOODS,
            "value" => 28.280507791758872
        ),

    "5" => Array
        (
            "BUSINESS_NAME" => FOOD AND GROCERY,
            "Division_name" => FRUIT AND VEG,
            "value" => 0.00004250371522120021
        ),

    "6" => Array
        (
            "BUSINESS_NAME" => FOOD AND GROCERY,
            "Division_name" => LIQUOR AND TOBACCO,
            "value" => 0.0001807993856424188
        ),

    "7" => Array
        (
            "BUSINESS_NAME" => FOOD AND GROCERY,
            "Division_name" => STAPLES,
            "value" => 63.63833077404931
        ),

    "8" => Array
        (
            "BUSINESS_NAME" => NONFOOD,
            "Division_name" => APPARELS,
            "value" => 0.009254311711678597
        ),

    "9" => Array
        (
            "BUSINESS_NAME" => NONFOOD,
            "Division_name" => ELECTRONICS,
            "value" => 0.0009884492728213818
        ),

    "10" => Array
        (
            "BUSINESS_NAME" => NONFOOD,
            "Division_name" => GENERALMERCHANDISING,
            "value" => 0.051390163621477414
        ),

    "11" => Array
        (
            "BUSINESS_NAME" => NONFOOD,
            "Division_name" => MAX,
            "value" => 0.23221365584864856
        ),

    "12" => Array
        (
            "BUSINESS_NAME" => NONFOOD,
            "Division_name" => TEXTILES,
            "value" => 0.01723803195136225
        )

);
?>

我需要动态地像这样:

"Bussines_Name" => array(

     "0" => array(

             "series_name" => Food and Grocery(

                 "value" => 23,
                 "value" => 34,
                 "value" => 45
                )    
        ),
     "1" => array(

           "series_name" => Non Food(

                 "value" => 33,
                 "value" => 44,
                 "value" => 55
                )    
        ),    
    );

1 个答案:

答案 0 :(得分:0)

您想要的输出似乎不是有效数组。例如,数组不能容纳多个具有相同名称的键

然而,以下是我的猜测:

DEMO

Array
(
    [BUSINESS_NAME] => Array
        (
            [0] => Array
                (
                    [series_name] => Array
                        (
                            [FOOD AND GROCERY] => Array
                                (
                                    [0] => 0.00094776941105184
                                    [1] => 0.005450625689262
                                    [2] => 2.1331155586759E-5
                                    [3] => 7.7634337924291
                                    [4] => 28.280507791759
                                    [5] => 4.25037152212E-5
                                    [6] => 0.00018079938564242
                                    [7] => 63.638330774049
                                )

                        )

                )

            [1] => Array
                (
                    [series_name] => Array
                        (
                            [NONFOOD] => Array
                                (
                                    [0] => 0.0092543117116786
                                    [1] => 0.00098844927282138
                                    [2] => 0.051390163621477
                                    [3] => 0.23221365584865
                                    [4] => 0.017238031951362
                                )

                        )

                )

        )

)

所以PHP代码是:

foreach ($arrFirst as $value) {
    $businesArr[$value['BUSINESS_NAME']][] = $value['value'];
}

foreach ($businesArr as $key => $value) {
    $finalArr['BUSINESS_NAME'][]['series_name'][$key] = $value;
}

print_r($finalArr);

DEMO