Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中,如何重新排列响应数据_Php_Arrays_Postgresql - Fatal编程技术网

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

在PHP中,如何重新排列响应数据,php,arrays,postgresql,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, billingfirst

我正在尝试使用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
如何对账单、评论和交易项目进行分组。请分享你的宝贵想法,这将有助于发展脚本


提前谢谢

您可以尝试以下方法:

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

重新创建数组时,
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;
    }

}

您希望数据库的结果是这样的吗?或者你只是想在得到结果后重新排列数组?@SugumarVenkatesan,谢谢你的快速回复!我想在获得数据后重新安排数据results@Anand最好将账单数据作为json对象存储在数据库中,这将使您的工作变得非常简单,你可以使用一些正则表达式来构造一个函数。你对事务所做的是我建议你做的更好的例子。
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;
        }
}