在php中自定义数组多维

时间:2015-11-25 10:49:40

标签: php arrays multidimensional-array

我有一个数组多维问题。我想像下面这样对数组结果进行cuztomize:

> Array (
>     [data] => Array
>         (
>           [nmproduk] => ABRI
>           (
>             [0] => Array
>                 (
>                     [regional] => LAINNYA
>                     [nmproduk] => ABRI
>                     [daily_peserta] => 0
>                     [monthly_peserta] => 0
>                     [yearly_peserta] => 30
>                     [daily_kredit] => 
>                     [monthly_kredit] => 
>                     [yearly_kredit] => 1630000000
>                     [daily_premi] => 
>                     [monthly_premi] => 
>                     [yearly_premi] => 17285943
>                 )
> 
>             [1] => Array
>                 (
>                     [regional] => REGIONAL I
>                     [nmproduk] => ABRI
>                     [daily_peserta] => 46
>                     [monthly_peserta] => 46
>                     [yearly_peserta] => 330
>                     [daily_kredit] => 3738000000
>                     [monthly_kredit] => 3738000000
>                     [yearly_kredit] => 26872000000
>                     [daily_premi] => 38769646
>                     [monthly_premi] => 38769646
>                     [yearly_premi] => 286798366
>                 )
> 
>             [2] => Array
>                 (
>                     [regional] => REGIONAL III
>                     [nmproduk] => ABRI
>                     [daily_peserta] => 0
>                     [monthly_peserta] => 0
>                     [yearly_peserta] => 41
>                     [daily_kredit] => 
>                     [monthly_kredit] => 
>                     [yearly_kredit] => 3658000000
>                     [daily_premi] => 
>                     [monthly_premi] => 
>                     [yearly_premi] => 41277232
>                 )
>             [3] => Array
>                 (
>                     [regional] => REGIONAL IV
>                     [nmproduk] => ABRI
>                     [daily_peserta] => 0
>                     [monthly_peserta] => 0
>                     [yearly_peserta] => 66
>                     [daily_kredit] => 
>                     [monthly_kredit] => 
>                     [yearly_kredit] => 7040000000
>                     [daily_premi] => 
>                     [monthly_premi] => 
>                     [yearly_premi] => 82187930
>                 )
>           ),
>           [todal_daily_peserta] => 46,
>           [total_monthly_peserta] => 46,
>           [total_yearly_perserta] => 437,
>           [todal_daily_kredit] => ???,
>           [total_monthly_kredit] => ???,
>           [total_yearly_kredit] => ???,
>           [total_daily_premi] => ???,
>           [total_monthly_premi] => ???,
>           [total_yearly_premi] => ???,
>           
>           
>           [nmproduk] => PERCEPATAN
>           (
>           [4] => Array
>               (
>                   [regional] => LAINNYA
>                   [nmproduk] => PERCEPATAN
>                   [daily_peserta] => 0
>                   [monthly_peserta] => 0
>                   [yearly_peserta] => 1875
>                   [daily_kredit] => 
>                   [monthly_kredit] => 
>                   [yearly_kredit] => 219962745000
>                   [daily_premi] => 
>                   [monthly_premi] => 
>                   [yearly_premi] => 7006117997.375
>               )
> 
>           [5] => Array
>               (
>                   [regional] => REGIONAL I
>                   [nmproduk] => PERCEPATAN
>                   [daily_peserta] => 1
>                   [monthly_peserta] => 1
>                   [yearly_peserta] => 927
>                   [daily_kredit] => 75000000
>                   [monthly_kredit] => 75000000
>                   [yearly_kredit] => 115814199000
>                   [daily_premi] => 721455
>                   [monthly_premi] => 721455
>                   [yearly_premi] => 3950688767.026
>               )
> 
>           [6] => Array
>               (
>                   [regional] => REGIONAL II
>                   [nmproduk] => PERCEPATAN
>                   [daily_peserta] => 6
>                   [monthly_peserta] => 6
>                   [yearly_peserta] => 1040
>                   [daily_kredit] => 823200000
>                   [monthly_kredit] => 823200000
>                   [yearly_kredit] => 123555350000
>                   [daily_premi] => 32251012.84
>                   [monthly_premi] => 32251012.84
>                   [yearly_premi] => 4150526573.54
>               )
> 
>           [7] => Array
>               (
>                   [regional] => REGIONAL III
>                   [nmproduk] => PERCEPATAN
>                   [daily_peserta] => 5
>                   [monthly_peserta] => 5
>                   [yearly_peserta] => 3738
>                   [daily_kredit] => 330500000
>                   [monthly_kredit] => 330500000
>                   [yearly_kredit] => 436641012000
>                   [daily_premi] => 13093625.45
>                   [monthly_premi] => 13093625.45
>                   [yearly_premi] => 14251701550.695
>               )
> 
>           [8] => Array
>               (
>                   [regional] => REGIONAL IV
>                   [nmproduk] => PERCEPATAN
>                   [daily_peserta] => 92
>                   [monthly_peserta] => 92
>                   [yearly_peserta] => 7946
>                   [daily_kredit] => 11069000000
>                   [monthly_kredit] => 11069000000
>                   [yearly_kredit] => 911394600000
>                   [daily_premi] => 323911350.2000001
>                   [monthly_premi] => 323911350.2000001
>                   [yearly_premi] => 28520524406.100006
>               )
>           ),
>           [todal_daily_peserta] => 46,
>           [total_monthly_peserta] => 46,
>           [total_yearly_perserta] => 437,
>           [todal_daily_kredit] => ???,
>           [total_monthly_kredit] => ???,
>           [total_yearly_kredit] => ???,
>           [total_daily_premi] => ???,
>           [total_monthly_premi] => ???,
>           [total_yearly_premi] => ???
> 
>         )
> 
> )

如果上面的数组是错误的,那么这个表应该是。每个产品名称在每个coloumn中有总计(总和)。我不知道这是否可能

product name : ABRI
country       | daily_peserta | daily_kredit | daily_premi | monthly_peserta | monthly_kredit | monthly_premi | yearly_peserta | yearly_kredit | yearly_premi |
LAINNYA       | 0             | ???          | ???         | ???             | ???            | ???           | ???            | ???           | ???          |
REGIONAL I    | 46            | ???          | ???         | ???             | ???            | ???           | ???            | ???           | ???          |
REGIONAL III  | 0             | ???          | ???         | ???             | ???            | ???           | ???            | ???           | ???          |
REGIONAL IV   | 0             | ???          | ???         | ???             | ???            | ???           | ???            | ???           | ???          |
TOTAL(SUM)    | 46            | ???          | ???         | ???             | ???            | ???           | ???            | ???           | ???          |

product name : PERCEPATAN
country       | daily_peserta | daily_kredit | daily_premi | monthly_peserta | monthly_kredit | monthly_premi | yearly_peserta | yearly_kredit | yearly_premi |
LAINNYA       | 0             | ???          | ???         | ???             | ???            | ???           | ???            | ???           | ???          |
REGIONAL I    | 46            | ???          | ???         | ???             | ???            | ???           | ???            | ???           | ???          |
REGIONAL III  | 0             | ???          | ???         | ???             | ???            | ???           | ???            | ???           | ???          |
REGIONAL IV   | 0             | ???          | ???         | ???             | ???            | ???           | ???            | ???           | ???          |
TOTAL(SUM)    | 46            | ???          | ???         | ???             | ???            | ???           | ???            | ???           | ???          |

但是我得到的结果如下所示,

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [regional] => LAINNYA
                    [nmproduk] => ABRI
                    [daily_peserta] => 0
                    [monthly_peserta] => 0
                    [yearly_peserta] => 30
                    [daily_kredit] => 
                    [monthly_kredit] => 
                    [yearly_kredit] => 1630000000
                    [daily_premi] => 
                    [monthly_premi] => 
                    [yearly_premi] => 17285943
                )

            [1] => Array
                (
                    [regional] => REGIONAL I
                    [nmproduk] => ABRI
                    [daily_peserta] => 46
                    [monthly_peserta] => 46
                    [yearly_peserta] => 330
                    [daily_kredit] => 3738000000
                    [monthly_kredit] => 3738000000
                    [yearly_kredit] => 26872000000
                    [daily_premi] => 38769646
                    [monthly_premi] => 38769646
                    [yearly_premi] => 286798366
                )

            [2] => Array
                (
                    [regional] => REGIONAL III
                    [nmproduk] => ABRI
                    [daily_peserta] => 0
                    [monthly_peserta] => 0
                    [yearly_peserta] => 41
                    [daily_kredit] => 
                    [monthly_kredit] => 
                    [yearly_kredit] => 3658000000
                    [daily_premi] => 
                    [monthly_premi] => 
                    [yearly_premi] => 41277232
                )

            [3] => Array
                (
                    [regional] => REGIONAL IV
                    [nmproduk] => ABRI
                    [daily_peserta] => 0
                    [monthly_peserta] => 0
                    [yearly_peserta] => 66
                    [daily_kredit] => 
                    [monthly_kredit] => 
                    [yearly_kredit] => 7040000000
                    [daily_premi] => 
                    [monthly_premi] => 
                    [yearly_premi] => 82187930
                )

            [4] => Array
                (
                    [regional] => LAINNYA
                    [nmproduk] => PERCEPATAN
                    [daily_peserta] => 0
                    [monthly_peserta] => 0
                    [yearly_peserta] => 1875
                    [daily_kredit] => 
                    [monthly_kredit] => 
                    [yearly_kredit] => 219962745000
                    [daily_premi] => 
                    [monthly_premi] => 
                    [yearly_premi] => 7006117997.375
                )

            [5] => Array
                (
                    [regional] => REGIONAL I
                    [nmproduk] => PERCEPATAN
                    [daily_peserta] => 1
                    [monthly_peserta] => 1
                    [yearly_peserta] => 927
                    [daily_kredit] => 75000000
                    [monthly_kredit] => 75000000
                    [yearly_kredit] => 115814199000
                    [daily_premi] => 721455
                    [monthly_premi] => 721455
                    [yearly_premi] => 3950688767.026
                )

            [6] => Array
                (
                    [regional] => REGIONAL II
                    [nmproduk] => PERCEPATAN
                    [daily_peserta] => 6
                    [monthly_peserta] => 6
                    [yearly_peserta] => 1040
                    [daily_kredit] => 823200000
                    [monthly_kredit] => 823200000
                    [yearly_kredit] => 123555350000
                    [daily_premi] => 32251012.84
                    [monthly_premi] => 32251012.84
                    [yearly_premi] => 4150526573.54
                )

            [7] => Array
                (
                    [regional] => REGIONAL III
                    [nmproduk] => PERCEPATAN
                    [daily_peserta] => 5
                    [monthly_peserta] => 5
                    [yearly_peserta] => 3738
                    [daily_kredit] => 330500000
                    [monthly_kredit] => 330500000
                    [yearly_kredit] => 436641012000
                    [daily_premi] => 13093625.45
                    [monthly_premi] => 13093625.45
                    [yearly_premi] => 14251701550.695
                )

            [8] => Array
                (
                    [regional] => REGIONAL IV
                    [nmproduk] => PERCEPATAN
                    [daily_peserta] => 92
                    [monthly_peserta] => 92
                    [yearly_peserta] => 7946
                    [daily_kredit] => 11069000000
                    [monthly_kredit] => 11069000000
                    [yearly_kredit] => 911394600000
                    [daily_premi] => 323911350.2000001
                    [monthly_premi] => 323911350.2000001
                    [yearly_premi] => 28520524406.100006
                )

            [9] => Array
                (
                    [regional] => LAINNYA
                    [nmproduk] => SPK REGULER
                    [daily_peserta] => 13
                    [monthly_peserta] => 13
                    [yearly_peserta] => 309
                    [daily_kredit] => 606950000
                    [monthly_kredit] => 606950000
                    [yearly_kredit] => 25774340000
                    [daily_premi] => 32376923.700000003
                    [monthly_premi] => 32376923.700000003
                    [yearly_premi] => 1112584745.1232
                )

            [10] => Array
                (
                    [regional] => REGIONAL I
                    [nmproduk] => SPK REGULER
                    [daily_peserta] => 27
                    [monthly_peserta] => 27
                    [yearly_peserta] => 360
                    [daily_kredit] => 2154000000
                    [monthly_kredit] => 2154000000
                    [yearly_kredit] => 26114650000
                    [daily_premi] => 97020498.19
                    [monthly_premi] => 97020498.19
                    [yearly_premi] => 1076010850.1599998
                )

            [11] => Array
                (
                    [regional] => REGIONAL II
                    [nmproduk] => SPK REGULER
                    [daily_peserta] => 28
                    [monthly_peserta] => 28
                    [yearly_peserta] => 1116
                    [daily_kredit] => 2675000000
                    [monthly_kredit] => 2675000000
                    [yearly_kredit] => 135934580000
                    [daily_premi] => 91049751.98
                    [monthly_premi] => 91049751.98
                    [yearly_premi] => 5204907049.508249
                )

            [12] => Array
                (
                    [regional] => REGIONAL III
                    [nmproduk] => SPK REGULER
                    [daily_peserta] => 30
                    [monthly_peserta] => 30
                    [yearly_peserta] => 659
                    [daily_kredit] => 2397900000
                    [monthly_kredit] => 2397900000
                    [yearly_kredit] => 67650500000
                    [daily_premi] => 96396448.16399999
                    [monthly_premi] => 96396448.16399999
                    [yearly_premi] => 2981944555.2765
                )

            [13] => Array
                (
                    [regional] => REGIONAL IV
                    [nmproduk] => SPK REGULER
                    [daily_peserta] => 94
                    [monthly_peserta] => 94
                    [yearly_peserta] => 1646
                    [daily_kredit] => 10647200000
                    [monthly_kredit] => 10647200000
                    [yearly_kredit] => 220800200000
                    [daily_premi] => 440534803.87
                    [monthly_premi] => 440534803.87
                    [yearly_premi] => 9380550993.34
                )

        )

)

我这个代码不使用任何框架只是本机的PHP,因为我只是继续我不知道的人。这里的代码。

            $result          = query_db($querySelect);
            while ($getData = mysql_fetch_assoc($result))
            {
                $data['data'][] = $getData;
            }



            if($result)
            {
                foreach ($data as $row) {
                    $arr = array("regional" => $row["regional"],
                                                    "Nama Produk" => $row["nmproduk"],
                                                    "Daily Peserta" => $row["daily_peserta"]);
                }
                print_r($arr);
                exit();
                echo json_encode($arr);
            } else {
                $json['err_no'] = '1';
                $json['err_msg'] = 'Error occured. Please try again.';
                echo json_encode($json);

}

这里是使用volkerk方式的结果,但我有问题如何汇总/总和每天_ ????值

{
  "data": {
    "nmproduk": [
      {
        "nama produk": "ABRI",
        "regional": "LAINNYA",
        "daily peserta": "0"
      },
      {
        "nama produk": "ABRI",
        "regional": "LAINNYA",
        "daily kredit": null
      },
      {
        "nama produk": "ABRI",
        "regional": "LAINNYA",
        "daily premi": null
      },
      {
        "nama produk": "ABRI",
        "regional": "REGIONAL I",
        "daily peserta": "46"
      },
      {
        "nama produk": "ABRI",
        "regional": "REGIONAL I",
        "daily kredit": "3738000000"
      },
      {
        "nama produk": "ABRI",
        "regional": "REGIONAL I",
        "daily premi": "38769646"
      },
      {
        "nama produk": "ABRI",
        "regional": "REGIONAL III",
        "daily peserta": "0"
      },
      {
        "nama produk": "ABRI",
        "regional": "REGIONAL III",
        "daily kredit": null
      },
      {
        "nama produk": "ABRI",
        "regional": "REGIONAL III",
        "daily premi": null
      },
      {
        "nama produk": "ABRI",
        "regional": "REGIONAL IV",
        "daily peserta": "0"
      },
      {
        "nama produk": "ABRI",
        "regional": "REGIONAL IV",
        "daily kredit": null
      },
      {
        "nama produk": "ABRI",
        "regional": "REGIONAL IV",
        "daily premi": null
      },
      {
        "nama produk": "PERCEPATAN",
        "regional": "LAINNYA",
        "daily peserta": "0"
      },
      {
        "nama produk": "PERCEPATAN",
        "regional": "LAINNYA",
        "daily kredit": null
      },
      {
        "nama produk": "PERCEPATAN",
        "regional": "LAINNYA",
        "daily premi": null
      },
      {
        "nama produk": "PERCEPATAN",
        "regional": "REGIONAL I",
        "daily peserta": "1"
      },
      {
        "nama produk": "PERCEPATAN",
        "regional": "REGIONAL I",
        "daily kredit": "75000000"
      },
      {
        "nama produk": "PERCEPATAN",
        "regional": "REGIONAL I",
        "daily premi": "721455"
      },
      {
        "nama produk": "PERCEPATAN",
        "regional": "REGIONAL II",
        "daily peserta": "6"
      },
      {
        "nama produk": "PERCEPATAN",
        "regional": "REGIONAL II",
        "daily kredit": "823200000"
      },
      {
        "nama produk": "PERCEPATAN",
        "regional": "REGIONAL II",
        "daily premi": "32251012.84"
      },
      {
        "nama produk": "PERCEPATAN",
        "regional": "REGIONAL III",
        "daily peserta": "5"
      },
      {
        "nama produk": "PERCEPATAN",
        "regional": "REGIONAL III",
        "daily kredit": "330500000"
      },
      {
        "nama produk": "PERCEPATAN",
        "regional": "REGIONAL III",
        "daily premi": "13093625.45"
      },
      {
        "nama produk": "PERCEPATAN",
        "regional": "REGIONAL IV",
        "daily peserta": "92"
      },
      {
        "nama produk": "PERCEPATAN",
        "regional": "REGIONAL IV",
        "daily kredit": "11069000000"
      },
      {
        "nama produk": "PERCEPATAN",
        "regional": "REGIONAL IV",
        "daily premi": "323911350.2000001"
      },
      {
        "nama produk": "SPK REGULER",
        "regional": "LAINNYA",
        "daily peserta": "13"
      },
      {
        "nama produk": "SPK REGULER",
        "regional": "LAINNYA",
        "daily kredit": "606950000"
      },
      {
        "nama produk": "SPK REGULER",
        "regional": "LAINNYA",
        "daily premi": "32376923.700000003"
      },
      {
        "nama produk": "SPK REGULER",
        "regional": "REGIONAL I",
        "daily peserta": "27"
      },
      {
        "nama produk": "SPK REGULER",
        "regional": "REGIONAL I",
        "daily kredit": "2154000000"
      },
      {
        "nama produk": "SPK REGULER",
        "regional": "REGIONAL I",
        "daily premi": "97020498.19"
      },
      {
        "nama produk": "SPK REGULER",
        "regional": "REGIONAL II",
        "daily peserta": "28"
      },
      {
        "nama produk": "SPK REGULER",
        "regional": "REGIONAL II",
        "daily kredit": "2675000000"
      },
      {
        "nama produk": "SPK REGULER",
        "regional": "REGIONAL II",
        "daily premi": "91049751.98"
      },
      {
        "nama produk": "SPK REGULER",
        "regional": "REGIONAL III",
        "daily peserta": "30"
      },
      {
        "nama produk": "SPK REGULER",
        "regional": "REGIONAL III",
        "daily kredit": "2397900000"
      },
      {
        "nama produk": "SPK REGULER",
        "regional": "REGIONAL III",
        "daily premi": "96396448.16399999"
      },
      {
        "nama produk": "SPK REGULER",
        "regional": "REGIONAL IV",
        "daily peserta": "94"
      },
      {
        "nama produk": "SPK REGULER",
        "regional": "REGIONAL IV",
        "daily kredit": "10647200000"
      },
      {
        "nama produk": "SPK REGULER",
        "regional": "REGIONAL IV",
        "daily premi": "440534803.87"
      }
    ]
  }
}

我已经对数组结果进行了排序,因为stackoverflow在创建一个帖子时有限制,我只是每天只显示不显示每月和每年。我希望你们能理解我的威胁。谢谢

1 个答案:

答案 0 :(得分:0)

$len=count($data);

    for($i=0;$i<$len;$i++) {
         $nmproduk=$data[$i]['nmproduk'];
         if(isset($data[$nmproduk])){
              array_push($data[$nmproduk],$data[$i]);
            }
          else
           {
               $data[$nmproduk][]=$data[$i];
          }

    }

它将返回使用通用名称nmproduk

索引的完整数组