Jump to content

Creating multidimensional array from variables


rodfarrell

Recommended Posts

I need to extract shopping cart transaction information from a database, convert it to json format and send to another DB.  Because the fields are not a direct match the calculated values are first bing put into variables.  I'm extracting the line items from the database into an array and looping through the array to get each line item which I want to then insert into another array as a sub array.  By the way, I'm new at PHP so be gentle.  The first step I've coded as:

 

 foreach ($oit_array as $inner1) {

    //  Check type
    if (!is_array($inner1)) {$inner1 = array($inner1);}
       $lineprice+=$inner1[orderitem_price];
  
//calculate amounts required in variables

  $linepriceinc=$inner1[orderitem_price]+($inner1[orderitem_tax]/$inner1[orderitem_quantity]);
if($inner1[orderitem_tax]==0){$linetaxrate+=0;}
  else $linetaxrate=($inner1[orderitem_final_price]-$inner1[orderitem_tax])/$inner1[orderitem_tax];
  $linetaxtotal+=$inner1[orderitem_tax];
  $orderqty+=$inner1[orderitem_quantity];
  $productname=$inner1[orderitem_name];
  $qtyshipped+=$inner1[orderitem_quantity];
$totallineamount+=$inner1[orderitem_final_price]-$inner1[orderitem_tax];
  $totallineamountinc+=$inner1[orderitem_final_price];
  $unitofmeasure="Units";
    
             
             $linearray[] = array("LinePrice"=>$lineprice,
                                                      "LinePriceInc"=>$linepriceinc,
                                                      "LineTaxCode"=>"GST",
                                                      "LineTaxRate"=> $linetaxrate,
                                                      "LineTaxTotal"=>  $linetaxtotal,
                                                      "OrderQty"=>  $orderqty,
                                                      "ProductName"=> $productname,
                                                      "QtyShipped"=> $orderqty,
                                                      "TotalLineAmount"=> $totallineamount,
                                                      "TotalLineAmountInc"=>$totallineamountinc,
                                                      "UnitOfMeasure"=>"Units");

 

I then reference the array as $linearray as follows:

 

 //Create Array
  $salesarray=
array ("type"=> "TCashSale",
       "fields"=>array ("CustomerName"=>$customername,
               "ShipToDesc"=>$shiptodesc,
               "Lines"=> array("type"=> "TCashSaleLine",
                        "fields"=> $linearray)),
                              "SalesPayments"=>array("type"=>"TSalesPayments",
                                       "fields"=>array("Amount"=> $amount,
                                                  "PayMethod"=> $paymentmethod)));

 

The problem that I have is that the resulting array contains square brackets around the "linearray" data like so:

 

{"type":"TCashSale","fields":{"CustomerName":"Cash Customer","ShipToDesc":"Rod Farrell\n\r1234 Short St\n\r\n\rSlackville Queensland 4001","Lines":{"type":"TCashSaleLine","fields":[{"LinePrice":54.54545,"LinePriceInc":54.54545,

 

What am I doing wrong??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.