自定义数组结构

时间:2016-07-26 09:20:05

标签: php

如何转换以下数组

Array
(
    [0] => Array
        (
            [order_num] => 1400236903
            [status] => unpaid
            [payment_rec_date] => 2014-05-16
            [description] => 
            [actual_amount] => 200 
            [stax] => 20.00
            [stax_value] =>30.00 
            [ed_tax] => 31.21
            [ed_tax_value] => 30.00
            [w_ed_tax] => 25.00
            [w_ed_tax_value] => 55.56 
            [amount] => 12000
            [total_discount] => 152
        )
   [0] => Array
        ( .....
         )
)

into 

Array
(
    [0] => Array
        (
            [order_num] => 1400236903
            [status] => unpaid
            [payment_rec_date] => 2014-05-16
            [description] => 
            [actual_amount] => 200 
        [data] => {"stax":20.00, "stax_value":30.00,"ed_tax":31.21,"ed_tax_value":30.00,"w_ed_tax":25.00..... }
        )
)

2 个答案:

答案 0 :(得分:1)

您必须使用foreach并检查要使用的密钥。 我使用json_encode()对您的数据进行编码。

解决方案

var blob = new Blob([document.getElementById('exportable').innerHTML], {
            type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
        });
        saveAs(blob, $scope.name+".xls");

Live example

答案 1 :(得分:-1)

您只需使用foreach中的array_walk()即可。那可以给你想要的结果。
第1部分:算法 - 没有Sample数组。 ( Quick Test it Here.
    <?php

    $arrResult  = array(); //$arr IS THE MAIN ARRAY TO CLEAN UP
    $arrFin     = array_walk($arr, function($data, $index) use (&$arrResult){
        $clone      = $data;
        $sifts      = array('stax', 'stax_value', 'ed_tax', 'ed_tax_value', 'w_ed_tax', 'w_ed_tax_value', 'amount', 'total_discount');
        $tempObj    = new stdClass();
        foreach($clone as $k=>$v){
            if(in_array($k, $sifts)){
                unset( $clone[$k] );
                $tempObj->$k = $v;
            }
        }
        $clone['data']  = $tempObj;
        $arrResult[]    = $clone;
    });

    var_dump($arrResult);
第2部分:算法 - 带样本数组。(Quick Test it Here.
<?php

    $arr        = array(
        array(
            'order_num'         =>1400236903,
            'status'            => 'unpaid',
            'payment_rec_date'  => '2014-05-16',
            'description'       => null,
            'actual_amount'     => 200,
            'stax'              => 20.00,
            'stax_value'        => 30.00,
            'ed_tax'            => 31.21,
            'ed_tax_value'      => 30.00,
            'w_ed_tax'          => 25.00,
            'w_ed_tax_value'    => 55.56,
            'amount'            => 12000,
            'total_discount'    => 152,
        ),

        array(
            'order_num'         =>3098786903,
            'status'            => 'paid',
            'payment_rec_date'  => '2014-05-16',
            'description'       => "Lorem Ipsum",
            'actual_amount'     => 200,
            'stax'              => 20.00,
            'stax_value'        => 30.00,
            'ed_tax'            => 31.21,
            'ed_tax_value'      => 30.00,
            'w_ed_tax'          => 25.00,
            'w_ed_tax_value'    => 55.56,
            'amount'            => 12000,
            'total_discount'    => 152,
        ),
    );

    $arrResult  = array();
    $arrFin     = array_walk($arr, function($data, $index) use (&$arrResult){
        $clone      = $data;
        $sifts      = array('stax', 'stax_value', 'ed_tax', 'ed_tax_value', 'w_ed_tax', 'w_ed_tax_value', 'amount', 'total_discount');
        $tempObj    = new stdClass();
        foreach($clone as $k=>$v){
            if(in_array($k, $sifts)){
                unset( $clone[$k] );
                $tempObj->$k = $v;
            }
        }
        $clone['data']  = $tempObj;
        $arrResult[]    = $clone;
    });

var_dump($arrResult);
上面的 var_dump($ arrResult)会产生以下输出:(Quick Test it Here.
    array (size=2)
      0 => 
        array (size=6)
          'order_num' => int 1400236903
          'status' => string 'unpaid' (length=6)
          'payment_rec_date' => string '2014-05-16' (length=10)
          'description' => null
          'actual_amount' => int 200
          'data' => 
            object(stdClass)[2]
              public 'stax' => float 20
              public 'stax_value' => float 30
              public 'ed_tax' => float 31.21
              public 'ed_tax_value' => float 30
              public 'w_ed_tax' => float 25
              public 'w_ed_tax_value' => float 55.56
              public 'amount' => int 12000
              public 'total_discount' => int 152
      1 => 
        array (size=6)
          'order_num' => int 3098786903
          'status' => string 'paid' (length=4)
          'payment_rec_date' => string '2014-05-16' (length=10)
          'description' => string 'Lorem Ipsum' (length=11)
          'actual_amount' => int 200
          'data' => 
            object(stdClass)[3]
              public 'stax' => float 20
              public 'stax_value' => float 30
              public 'ed_tax' => float 31.21
              public 'ed_tax_value' => float 30
              public 'w_ed_tax' => float 25
              public 'w_ed_tax_value' => float 55.56
              public 'amount' => int 12000
              public 'total_discount' => int 152