php问题从函数返回一个数组

时间:2018-06-03 02:16:21

标签: php

我通过联邦快递报价系统的卷曲来提取信息。我得到了必要的信息。卷曲功能正常。结果是准确的。 但是,当我尝试将结果添加到数组时,它不会将它们从函数传递到页面。它就像结果不存储在函数中一样。

foreach($result->SOAPENVBody->RateReply->RateReplyDetails as $value) {
    $number = ++$number;
    $key1 = $result->SOAPENVBody->RateReply->RateReplyDetails[$number]->ServiceType;
    $value1 = $result->SOAPENVBody->RateReply->RateReplyDetails[$number]->RatedShipmentDetails[0]->ShipmentRateDetail->TotalNetCharge->Amount;


    $options[$key1] = $value1;
}

    return $options;

我知道函数会像我一样通过数组设置

$key1 = '1';
$value1 = 'Hello';

它将该信息传递给该页面。

当我通过$ key1和$ value1(echo $key1 . $value1;)回显卷曲结果时,它也会在页面上显示正确的信息。它只是不会将它传递给阵列。

下面我将回显来自echo的结果以显示正在检索的信息

FIRST_OVERNIGHT: 194.35
PRIORITY_OVERNIGHT: 147.83
STANDARD_OVERNIGHT: 133.77
FEDEX_2_DAY_AM: 111.54
FEDEX_2_DAY: 99.78
FEDEX_EXPRESS_SAVER: 89.71
FEDEX_GROUND: 20.92

而且我想要有人想看看我如何调用函数......这里是来自调用函数的主页面。

include_once('inc/functions/fedex_rate.php');
          $fedex_options = array();
         $fedex_options = fedex_rate($totalweight);

          foreach ($fedex_options as $key => $value) {
              echo '<tr><td colspan="2"><div class="margin10">'. $key .'</div></td><td colspan="2"><div class="margin10">'. $value .'</div></td></tr>'; 
          }

我甚至尝试在主页面的foreach循环中使用$options;然而,这也没有用。我终于看到了一个例子,其中某人使用函数中的数组细节设置了一个新数组,这就是我停止的地方。

以下是联邦快递费率报价完整功能减去敏感信息

<?

function getProperty($var) {
if ($var == 'key') return 'xxxxxxxxx';
if ($var == 'password') return 'xxxxxxxxx';
if ($var == 'account') return 'xxxxxx';
if ($var == 'meter') return 'xxxxx';
}


function fedex_rate($totalweight) {
//your account details here
$key = getProperty('key');
$password = getProperty('password');;
$account_number = getProperty('account');
$meter_number = getProperty('meter');
$residential = '1'; // 1 = true, 0 = false
    if ($residential == 1) { $residential = 'true'; }
    if ($residential == 0) { $residential = 'false'; }
if($residential == 1) { $servicetype = 'GROUND_HOME_DELIVERY'; }
if ($residential == 0) { $servicetype = ''; }



$recipient_address = 'xxxxxxx';
$recipient_city = 'xxxxxx';
$recipient_state = 'xx';
$recipient_zip = 'xxxxx';
$recipient_county = 'xx';



$xml = '<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://fedex.com/ws/rate/v13"><SOAP-ENV:Body><ns1:RateRequest>
<ns1:WebAuthenticationDetail>
<ns1:UserCredential>
<ns1:Key>'.$key.'</ns1:Key>
<ns1:Password>'.$password.'</ns1:Password>
</ns1:UserCredential></ns1:WebAuthenticationDetail>
<ns1:ClientDetail>
<ns1:AccountNumber>'.$account_number.'</ns1:AccountNumber>
<ns1:MeterNumber>'.$meter_number.'</ns1:MeterNumber>
</ns1:ClientDetail>
<ns1:TransactionDetail><ns1:CustomerTransactionId> *** Rate Request v13 using PHP ***</ns1:CustomerTransactionId></ns1:TransactionDetail><ns1:Version><ns1:ServiceId>crs</ns1:ServiceId><ns1:Major>13</ns1:Major><ns1:Intermediate>0</ns1:Intermediate><ns1:Minor>0</ns1:Minor></ns1:Version><ns1:ReturnTransitAndCommit>true</ns1:ReturnTransitAndCommit>

<ns1:RequestedShipment>
<ns1:DropoffType>REGULAR_PICKUP</ns1:DropoffType>';
// add- if service type is selected, echo service type code.  if not, leave it out
//<ns1:ServiceType>'. $service_type .'</ns1:ServiceType>
$xml .= '<ns1:PackagingType>YOUR_PACKAGING</ns1:PackagingType>
<ns1:TotalInsuredValue>
<ns1:Currency>USD</ns1:Currency>
</ns1:TotalInsuredValue>

<ns1:Shipper>
<ns1:Contact>
<ns1:PersonName>Sender Name</ns1:PersonName>
<ns1:CompanyName>Sender Company Name</ns1:CompanyName>
<ns1:PhoneNumber></ns1:PhoneNumber>
</ns1:Contact>

<ns1:Address>
<ns1:StreetLines></ns1:StreetLines>
<ns1:City></ns1:City>
<ns1:StateOrProvinceCode></ns1:StateOrProvinceCode>
<ns1:PostalCode>xxxxxx</ns1:PostalCode>
<ns1:CountryCode>xx</ns1:CountryCode>
</ns1:Address>
</ns1:Shipper>

<ns1:Recipient>
<ns1:Contact>
<ns1:PersonName>Recipient Name</ns1:PersonName>
<ns1:CompanyName>Company Name</ns1:CompanyName>
<ns1:PhoneNumber></ns1:PhoneNumber>
</ns1:Contact>

<ns1:Address>
<ns1:StreetLines>'. $recipient_address .'</ns1:StreetLines>
<ns1:City>'. $recipient_city .'</ns1:City>
<ns1:StateOrProvinceCode>'. $recipient_state .'</ns1:StateOrProvinceCode>
<ns1:PostalCode>'. $recipient_zip .'</ns1:PostalCode>
<ns1:CountryCode>'. $recipient_county .'</ns1:CountryCode>
<ns1:Residential>'. $residential .'</ns1:Residential>
</ns1:Address>
</ns1:Recipient>

<ns1:ShippingChargesPayment>
<ns1:PaymentType>SENDER</ns1:PaymentType>
<ns1:Payor>
<ns1:ResponsibleParty>
<ns1:AccountNumber>'.$account_number.'</ns1:AccountNumber>
</ns1:ResponsibleParty>
</ns1:Payor>
</ns1:ShippingChargesPayment>

<ns1:RateRequestTypes>ACCOUNT</ns1:RateRequestTypes>

<ns1:PackageCount>1</ns1:PackageCount>
<ns1:RequestedPackageLineItems>

<ns1:SequenceNumber>1</ns1:SequenceNumber>

<ns1:GroupPackageCount>1</ns1:GroupPackageCount>

<ns1:Weight>
<ns1:Units>LB</ns1:Units>
<ns1:Value>'.$totalweight.'</ns1:Value>
</ns1:Weight>

<ns1:Dimensions>
<ns1:Length>10</ns1:Length>
<ns1:Width>10</ns1:Width>
<ns1:Height>10</ns1:Height>
<ns1:Units>IN</ns1:Units>
</ns1:Dimensions>

</ns1:RequestedPackageLineItems>

</ns1:RequestedShipment>
</ns1:RateRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://ws.fedex.com:443/web-services');
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$result_xml = curl_exec($ch);

// remove colons and dashes to simplify the xml
$result_xml = str_replace(array(':','-'), '', $result_xml);
$result = @simplexml_load_string($result_xml);

;


$number = -1;

foreach($result->SOAPENVBody->RateReply->RateReplyDetails as $value) {
    $number = ++$number;
    $key1 = $result->SOAPENVBody->RateReply->RateReplyDetails[$number]->ServiceType;
    $value1 = $result->SOAPENVBody->RateReply->RateReplyDetails[$number]->RatedShipmentDetails[0]->ShipmentRateDetail->TotalNetCharge->Amount;
    //echo $key1 .': '. $value1 .'<br />';

    $options[$key1] = $value1;
}

    return $options;

} // function

我能够通过在数组中的$ result之前和之后添加一个空值来使它工作......但为什么会这样呢?为什么我不能将$ result标记的值单独传递给数组?

$key1 = ''. $result->SOAPENVBody->RateReply->RateReplyDetails[$number]->ServiceType .'';

    $value1 = ''. $result->SOAPENVBody->RateReply->RateReplyDetails[$number]->RatedShipmentDetails[0]->ShipmentRateDetail->TotalNetCharge->Amount .'';

任何人都可以告诉我为什么我不能将$ result直接传递给数组而没有任何额外的空白标签?这最终成为了问题所在,因此找出答案的人会得到接受的答案。

3 个答案:

答案 0 :(得分:2)

解析XML后,每个元素都是SimpleXMLElement对象,对象不能设置为数组中的键

当你连接空字符串时,它变成了字符串而不是Object。如您所知,当您尝试回显对象时,它会调用__toString方法(如果已定义)。

如果您想调试此问题,请尝试致电,

var_dump($result->SOAPENVBody->RateReply->RateReplyDetails[$number]->ServiceType)

您也可以通过以下方式解决此问题:

$key1 = (string)$result->SOAPENVBody->RateReply->RateReplyDetails[$number]->ServiceType;

OR

$key1 = $result->SOAPENVBody->RateReply->RateReplyDetails[$number]->ServiceType->__toString();

答案 1 :(得分:0)

问题是之前在fedex_rate()中没有定义$ options。只需在foreach之前将其定义为空数组。

 $options = array();
 foreach($result->SOAPENVBody->RateReply->RateReplyDetails as $value) {
    ....

答案 2 :(得分:0)

我能够通过在数组中的$ result之前和之后添加一个空值来实现它... 但是为什么会出现这种情况?为什么我无法传递值$ result标记自己的数组?

$key1 = ''. $result->SOAPENVBody->RateReply->RateReplyDetails[$number]->ServiceType .'';

    $value1 = ''. $result->SOAPENVBody->RateReply->RateReplyDetails[$number]->RatedShipmentDetails[0]->ShipmentRateDetail->TotalNetCharge->Amount .'';