在PHP中如何重新排列响应数据

时间:2015-11-27 12:22:13

标签: php arrays postgresql

我正在尝试使用PHP重新安排psql的响应数据。到目前为止,我创建了这个PHP脚本。

$host = 'localhost';
$user = 'username';
$pass = 'pwd';
$db = 'prod_db';
$query = "select id, invoicenumber, customerid, orderstatusid, orderdiscount, salestax, salestax2, salestax3, orderamount, billingfirstname, billinglastname, billingcompany, billingaddress, billingaddress2, billingcity, billingstate, billingzipcode, billingcountry, billingphonenumber, billingemail, billingpaymentmethod, billingonlinepayment, billingpaymetmethodid, customercomments, internalcomments, externalcomments, cardtype, referer, ip, userid, lastupdate, affiliatecommision, transactionlist, orderitemlist, shipmentlist, questionlist from _3DCartOrders";

    $con = pg_connect ("dbname=$db user=$user");

    $result = pg_query($con, $query); 

    $aa = pg_fetch_all($result);

    print_r($aa); die();

我得到了这样的结果。

[0] => Array
        (
            [id] => 117521862
            [invoicenumber] => 62795
            [customerid] => 0
            [orderstatusid] => 4
            [orderdiscount] => 
            [salestax] => 
            [salestax2] => 
            [salestax3] => 
            [orderamount] => 232.16
            [billingfirstname] => David
            [billinglastname] => B
            [billingemail] => 
            [billingaddress] => 1408 PURDY AVE
            [billingaddress2] => 
            [billingcity] => Chandler
            [billingstate] => Arizona
            [billingzipcode] => 85224
            [billingcountry] => US
            [billingphone] => 6026808507
            [billingemail] => anand@jjbee.com
            [billingcompany] => 
            [customercomments] => Please send item as gift.  Do not put receipt outside the box.
            [internalcomments] => Do not put receipt outside the box.
            [externalcomments] => Do not put receipt outside the box.
            [transactionlist] => {"{\"OrderID\": 34, \"TransactionID\": \"543\", \"TransactionAVS\": \"\", \"TransactionCVV2\": \"\", \"TransactionType\": \"CA\", \"TransactionAmount\": 2.16, \"TransactionMethod\": \" CBA - New Order\", \"TransactionIndexID\": 562, \"TransactionApproval\": \"c56954\", \"TransactionCaptured\": 0, \"TransactionDateTime\": \"2015-05-20T18:36:56\", \"TransactionGatewayID\": 00, \"TransactionReference\": \"\", \"TransactionResponseCode\": \"\", \"TransactionResponseText\": \"\"}","{\"OrderID\": 284, \"TransactionID\": \"6150\", \"TransactionAVS\": \"\", \"TransactionCVV2\": \"\", \"TransactionType\": \"CA\", \"TransactionAmount\": 232.16, \"TransactionMethod\": \"Amazon CBA - Ready to Ship\", \"TransactionIndexID\": 57666, \"TransactionApproval\": \"a6846a\", \"TransactionCaptured\": 0, \"TransactionDateTime\": \"2015-05-20T18:51:42\", \"TransactionGatewayID\": 400, \"TransactionReference\": \"\", \"TransactionResponseCode\": \"\", \"TransactionResponseText\": \"\"}"}
            [discount] => 20.00

        )

预期产出:

[0] => Array
        (
            [id] => 117521862
            [invoicenumber] => 62795
            [customerid] => 0
            [orderstatusid] => 4
            [orderdiscount] => 
            [salestax] => 0.00
            [salestax2] => 0.00
            [salestax3] => 0.00
            [BillingAddress] => Array
                (
                    [FirstName] => Fiona
                    [LastName] => Mak
                    [Email] => fion@ddfail.com
                    [Address] => 28 Byng Ave
                    [Address2] => Unit 1509
                    [City] => Toronto
                    [ZipCode] => M2N 7H4
                    [StateCode] => ON
                    [CountryCode] => CA
                    [Phone] => 4168782690
                    [Company] => 

                )

            [Comments] => Array
                (
                    [OrderComment] => Please send item as gift.  Do not put receipt outside the box, put it inside the box
                    [OrderInternalComment] => Please send item as gift.  Do not put receipt outside the box.
                    [OrderExternalComment] => 11/15/2015 by Automated Script
                )

            [PaymentMethod] => Secure Online Payment
            [CardType] => Mastercard
            [Time] => 5:02:38 PM
            [Transaction] => Array
                (
                    [CVV2] => Pass
                    [ResponseText] => 
                    [AVS] => Street-Fail/Zip-Pass
                    [TransactionId] => PG056191
                    [ApprovalCode] => 071066
                    [TransactionType] => Sale
                    [Amount] => 206.61
                )

            [Discount] => 20.00

如何对结算,评论和交易项目进行分组。请分享您有价值的想法,这将有助于开发脚本。

提前致谢!

3 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

$billing["billingstuff"]=$aa["billingstuff"];
unset($aa["billingstuff"]);
//And so on with the billing stuff...

$aa["BillingAddress"] = $billing;

同样的评论。

对于transactionlist,因为它是JSON编码的:

$transaction = json_decode($aa["transaction"], true));
$aa["Transaction"] = $transaction;

答案 1 :(得分:0)

使用第二个参数重新创建数组json_decode会将其转换为数组而不是对象,以便获得数组。

$newAa = array();
foreach ( $aa as $columName => columnValue ) {
    if ( strstr( $columnName, 'billing' ) ) {
        $newAa[ 'BillingAddress' ][$columnName] = $columnValue;
    } elseif ( strstr( $columName, 'comments' ) ) {
        $newAa[ 'Comments' ][$columnName] = $columnValue;
    } elseif ( strstr( $columName, 'transactionlist' ) ) {
        $newAa[ 'Transaction' ] = json_decode( $columnValue, true );
    } else {
        $newAa[ $columName ] = $columnValue;
    }

}

答案 2 :(得分:0)

foreach($yourarray as $key => $element){
        if($key == 'FirstName' || $key == 'LastName' || $key == 'Email' || $key == 'Address' || $key == 'Address2' || $key == 'City' || $key == 'ZipCode' || $key == 'StateCode' || $key == 'CountryCode' || $key == 'Phone' || $key == 'Company'){
            $result['BillingAddress'][$key] = $element;
        } else if($key == 'OrderComment' || $key == 'OrderInternalComment' || $key == 'OrderExternalComment' ){
            $result['Comments'][$key] = $element;
        } else if ($key == 'CVV2' || $key == 'ResponseText' || $key == 'AVS' || $key == 'TransactionId' || $key == 'ApprovalCode' || $key == 'TransactionType' || $key == 'Amount'){
            $result['Transaction'][$key] = $element;
        } else {
            $result[$key] = $element;
        }
}