dpoxk64080 2019-02-26 14:06
浏览 47

如何存储变量并在foreach之外回显它以使用API​​ POST重用它

So i have foreach that is getting all the user data of ebay when its an international sale. It works fine while its in foreach but i dont know how to store the variable so when used outside foreach it outputs the same as in foreach ( does that make any sense ? )

if($order->IsMultiLegShipping != 'false'){

            $MultiLegShippingDetails = $order->MultiLegShippingDetails;
                foreach($MultiLegShippingDetails as $LegShippingDetails){
                    foreach($LegShippingDetails as $ShipmentToLogisticsProvider){
                        echo $ShipmentToLogisticsProvider->ShippingServiceDetails->ShippingService;
                        foreach($ShipmentToLogisticsProvider as $iAddress){
                            echo $iName = $iAddress->Name;
                            echo $iAddress->Street1;
                            echo $iAddress->Street2;
                            echo $iAddress->CityName;
                            echo $iAddress->CountryName;
                        }
                    }
                }
        }
        elseif($order->IsMultiLegShipping != 'true'){

                            echo $Name = $shippingAddress->Name;
                            echo $shippingAddress->Street1;
                            echo $shippingAddress->Street2;
                            echo $shippingAddress->CityName;
                            echo $shippingAddress->StateOrProvince;
                            echo $shippingAddress->PostalCode;
                            echo $shippingAddress->CountryName;
        }

When i will do echo $iName outside the foreach it will return empty even tho it has data in foreach. What am i doing wrong ?

foreach is written for this specific xml from eBay getOrders

<MultiLegShippingDetails> MultiLegShippingDetailsType
            <SellerShipmentToLogisticsProvider> MultiLegShipmentType
              <ShippingServiceDetails> MultiLegShippingServiceType
                <ShippingService> token </ShippingService>
                <TotalShippingCost currencyID="CurrencyCodeType"> AmountType (double) </TotalShippingCost>
              </ShippingServiceDetails>
              <ShippingTimeMax> int </ShippingTimeMax>
              <ShippingTimeMin> int </ShippingTimeMin>
              <ShipToAddress> AddressType
                <AddressAttribute type="AddressAttributeCodeType"> AddressAttributeType (string) </AddressAttribute>
                <!-- ... more AddressAttribute values allowed here ... -->
                <AddressID> string </AddressID>
                <AddressOwner> AddressOwnerCodeType </AddressOwner>
                <AddressUsage> AddressUsageCodeType </AddressUsage>
                <CityName> string </CityName>
                <Country> CountryCodeType </Country>
                <CountryName> string </CountryName>
                <ExternalAddressID> string </ExternalAddressID>
                <Name> string </Name>
                <Phone> string </Phone>
                <PostalCode> string </PostalCode>
                <ReferenceID> string </ReferenceID>
                <StateOrProvince> string </StateOrProvince>
                <Street1> string </Street1>
                <Street2> string </Street2>
              </ShipToAddress>
            </SellerShipmentToLogisticsProvider>
          </MultiLegShippingDetails>

this is my full code if someone needs it

<?php



include_once('includes.php');

$licence_key = '**'; //Your Licence Key here

/*  © 2013 eBay Inc., All Rights Reserved */ 
/* Licensed under CDDL 1.0 -  http://opensource.org/licenses/cddl1.php */
header("Content-Type: text/html; charset=UTF-8");
//$response = simplexml_import_dom($responseDoc);
if ($entries == 0) {
    echo "Sorry No entries found in the Time period requested. Change CreateTimeFrom/CreateTimeTo and Try again";
} else {
    $orders = $response->OrderArray->Order;
    if($orders != null){
    foreach($orders as $order){
        if (date("d/m/Y", strtotime($order->ShippedTime)) == '01/01/1970' 
                and $order->OrderStatus != 'Active' and $order->OrderStatus != 'Cancelled' ) {

            echo "Record -> ".$RecordID = $order->ShippingDetails->SellingManagerSalesRecordNumber."";

            echo "OrderID -> ". $OrderID = $order->OrderID . "";

            // get the amount paid
                $AmountPaid = $order->AmountPaid;
                $AmountPaidAttr = $AmountPaid->attributes();
                echo "AmountPaid : " . $AmountPaid . " "  .$AmountPaidAttr["currencyID"]. "<br>";

            $transactions = $order->TransactionArray;
            if($transactions){
                foreach($transactions->Transaction as $transaction){
                    echo "Item ID -> " . $transaction->Item->ItemID . "<br>";
                    $variations = $transaction->Variation;
                    echo "Item URL -> " . $variations->VariationViewItemURL . "<br>";
                    echo "Variation SKU : ".$transaction->Item->SKU."<br>";
                    echo "Variation SKU : ".$variations->SKU."<br>";
                    foreach($variations as $variation){
                        foreach($variation as $vspecs ){
                            foreach($vspecs as $nvl){
                                echo "".$nvl->Name.": ";
                                echo "".$nvl->Value."<br>";
                            }//foreach vspecs as nvl
                        }//foreach variation as vspecs
                    }//foreach variations as variation

                }//foreach transactions as transaction
            }//if transactions
            // get the checkout message left by the buyer, if any
                if (!empty($order->BuyerCheckoutMessage)) {
                    echo "<b>Checkout Message</b> : " . $order->BuyerCheckoutMessage . "<br>";
                }
            else{
                echo "Checkout Message: No Message left. Check Messages folder"."<br>";
            }//checkout message
            $BuyerEmail = $transaction->Buyer->Email;
                    if ($BuyerEmail == 'Invalid Request'){
                        echo "Buyer Email : Order is too old for email to be fetched" ."<br>";
                        }
                    else{
                            echo "Buyer Email : " .$BuyerEmail ."<br>";
                        }//buyer email
            echo "Buyer User ID : " .$order->BuyerUserID ."<br>";

            echo "International : ".$order->IsMultiLegShipping."<br>";

            $ShippingServiceSelected = $order->ShippingServiceSelected;
                if($ShippingServiceSelected){
                echo "Shipping Service Selected  : ".$ShippingServiceSelected->ShippingService." <br>";//Shipping service
                }//shipping


            $shippingAddress = $order->ShippingAddress;

            $Name = array();

            if($order->IsMultiLegShipping != 'false'){

                $MultiLegShippingDetails = $order->MultiLegShippingDetails;
                    foreach($MultiLegShippingDetails as $LegShippingDetails){
                        foreach($LegShippingDetails as $ShipmentToLogisticsProvider){
                            echo $ShipmentToLogisticsProvider->ShippingServiceDetails->ShippingService;
                            foreach($ShipmentToLogisticsProvider as $iAddress){
                                echo $Name = $iAddress->Name;
                                echo $iAddress->Street1;
                                echo $iAddress->Street2;
                                echo $iAddress->CityName;
                                echo $iAddress->CountryName;
                            }
                        }
                    }
            }
            elseif($order->IsMultiLegShipping != 'true'){

                                echo $Name = $shippingAddress->Name;
                                echo $shippingAddress->Street1;
                                echo $shippingAddress->Street2;
                                echo $shippingAddress->CityName;
                                echo $shippingAddress->StateOrProvince;
                                echo $shippingAddress->PostalCode;
                                echo $shippingAddress->CountryName;
            }




            echo "------<br>";

            //send email
            $to      = '***';
            $subject = 'New Orders';
            $message = '
            Order # '.$order->ShippingDetails->SellingManagerSalesRecordNumber.'
            Item ID: '.$transaction->Item->ItemID.'

            ';
            $headers = 'From: ***' . "
" .
            'Reply-To: ***' . "
" .
            'X-Mailer: PHP/' . phpversion();

            mail($to, $subject, $message, $headers);
            //end send email


            //Add Job
$job_products = [
    [
        "item_id"           => "212313",
        "item_name"         => "Product A",
        "item_code"         => "039018",
        "item_description"  => "Test:Carry out smoke or pressure test.",
        "cost_price"        => "21.09",
        "price"             => "32.44"
    ],
    [
        "item_id"           => "412325234",
        "item_name"         => "Product B",
        "item_code"         => "039018",
        "item_description"  => "Test:Carry out smoke or pressure test.",
        "cost_price"        => "10",
        "price"             => "50"
    ]
];

$data = [
    'completion_date'       =>  '31/03/2019',
    'customer_id'           =>  80,
    'full_name'             =>  $Name,
    'email_address'         =>  'email@email.com',
    'telephone'             =>  '012122212',
    'mobile'                =>  '0787878',
    'address'               =>  'Line 1 address'.chr(10).'Line 2 address',
    'city'                  =>  'City',
    'county'                =>  'County',
    'postcode'              =>  'Postcode',
    'site_company_name'     =>  'Site Company Name',
    'site_full_name'        =>  'Site Contact Name',
    'site_telephone'        =>  '012121212',
    'site_mobile'           =>  '07878787',
    'site_fax'              =>  'Depreciated, not in use',
    'site_email_address'    =>  'email@email.com',
    'site_address'          =>  'Site Line 1 address'.chr(10).'Line 2 address',
    'site_city'             =>  'Site City',
    'site_county'           =>  'Site County',
    'site_postcode'         =>  'Site Postcode',
    'site_notes'            =>  'Site Notes',
    'customer_ref'          =>  $RecordID,
    'wo_ref'                =>  'Customer Job Ref',
    'po_ref'                =>  $OrderID,
    'short_description'     =>  'short description of job',
    'description'           =>  'long description of job',
    'customer_notes'        =>  'Customer notes',
    'job_products'          =>  json_encode($job_products)
];

$response = postRequest($licence_key, 'add_job', $data);
print_r($response);


        }//if date and status matches
    }//foreach orders as order
    }//if orders null
    else{
        echo "No Orders";
    }//else no orders
}//end
?>

Preferably i need these details to be used in e.g 'full_name' => $Name,

  • 写回答

1条回答 默认 最新

  • doujingtang6580 2019-02-26 14:27
    关注

    Before the loop declare an array and then fill it with your information like :

    $res = array();
    
    foreach($MultiLegShippingDetails as $LegShippingDetails){
                        foreach($LegShippingDetails as $ShipmentToLogisticsProvider){
                            echo $ShipmentToLogisticsProvider->ShippingServiceDetails->ShippingService;
                            foreach($ShipmentToLogisticsProvider as $iAddress){
                                echo $iName = $iAddress->Name;
                                echo $iAddress->Street1;
                                echo $iAddress->Street2;
                                echo $iAddress->CityName;
                                echo $iAddress->CountryName;
                                 $res[] = array('fullName'=> $iAddress->Name , 'adress' => $iAddress->Street1); // etc
                            }
                        }
                    }
    

    At the end , you can print your array $res and you find out .

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向