PHP数组拒绝将数字字符串视为数值


PHP array refuses to treat numeric string as number value

我正试图将一个PHP数组输出到一个正数PHP变量中,这样我就可以对它进行算术运算,然后全局存储它,以便在购物车中使用。

以下是生成和操作数组的代码:

<?php
function ups($dest_zip,$service,$weight,$length,$width,$height) {
$AccessLicenseNumber = 'XXXXXXXXXXXXX'; // Your license number
$UserId = 'XXXXXXXX'; // Username
$Password = 'XXXXXXXX'; // Password
$PostalCode = 'XXXXXX'; // Zipcode you are shipping FROM
$ShipperNumber = 'XXXXX'; // Your UPS shipper number
    $data ="<?xml version='"1.0'"?>
    <AccessRequest xml:lang='"en-US'">
        <AccessLicenseNumber>$AccessLicenseNumber</AccessLicenseNumber>
        <UserId>$UserId</UserId>
        <Password>$Password</Password>
    </AccessRequest>
    <?xml version='"1.0'"?>
    <RatingServiceSelectionRequest xml:lang='"en-US'">
        <Request>
            <TransactionReference>
                <CustomerContext>Bare Bones Rate Request</CustomerContext>
                <XpciVersion>1.0001</XpciVersion>
            </TransactionReference>
            <RequestAction>Rate</RequestAction>
            <RequestOption>Rate</RequestOption>
        </Request>
    <PickupType>
        <Code>01</Code>
    </PickupType>
    <Shipment>
        <Shipper>
            <Address>
                <PostalCode>$PostalCode</PostalCode>
                <CountryCode>US</CountryCode>
            </Address>
        <ShipperNumber>$ShipperNumber</ShipperNumber>
        </Shipper>
        <ShipTo>
            <Address>
                <PostalCode>$dest_zip</PostalCode>
                <CountryCode>US</CountryCode>
            <ResidentialAddressIndicator/>
            </Address>
        </ShipTo>
        <ShipFrom>
            <Address>
                <PostalCode>$PostalCode</PostalCode>
                <CountryCode>US</CountryCode>
            </Address>
        </ShipFrom>
        <Service>
            <Code>$service</Code>
        </Service>
        <Package>
            <PackagingType>
                <Code>02</Code>
            </PackagingType>
            <Dimensions>
                <UnitOfMeasurement>
                    <Code>IN</Code>
                </UnitOfMeasurement>
                <Length>$length</Length>
                <Width>$width</Width>
                <Height>$height</Height>
            </Dimensions>
            <PackageWeight>
                <UnitOfMeasurement>
                    <Code>LBS</Code>
                </UnitOfMeasurement>
                <Weight>$weight</Weight>
            </PackageWeight>
        </Package>
    </Shipment>
    </RatingServiceSelectionRequest>";
    $ch = curl_init("https://www.ups.com/ups.app/xml/Rate");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_TIMEOUT, 60);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    $result=curl_exec ($ch);        
   $upsout = (explode('USD', $result, 15));    
    $handling = $upsout[3];
   echo "<br><br>Shipping: $";
    echo $handling; 
   echo " USD<br><br>";       
   curl_close($ch);
   var_dump( $handling);
// print_r ($upsout);}?>

$handling的输出总是一个00.00或000.00格式的数字,当然零是数字。我真的需要这一点,以便能够在算术函数中工作,并能够存储为数字。

我知道它不起作用,因为如果我做

$test = $handling *2;
echo $test;

我得到"0"作为输出,而不是两倍于$handling…

编辑:

如果我运行var_dump(htmlentities($handling));

输出为:

字符串(222)"33.35"

如果我运行var_dump($handling);

输出是

字符串(168)"33.35"

所以。。。。我需要找到一种方法来解析这个数组条目中的垃圾,这样我就可以用代数方法处理它。

我打赌你的字符串中有HTML标记,这就是为什么你会得到这个var_dump():

string(168) "33.35"

第一个数字表示字符串的长度为168个字符,而33.35占用5个字符。所以里面还有其他东西,不仅仅是一个数字。

其中一个解决方案会起作用,但我不能确定字符串的实际外观,所以我抛出了一堆想法。

$haystack0 = strip_tags( $haystack); 
echo '0. ' . $haystack0 * 2; echo "<br />'n";
$haystack1 = trim( strip_tags( $haystack)); 
echo '1. ' . $haystack1 * 2; echo "<br />'n";
preg_match( '/'b('d+'.'d+)'b/', $haystack, $match);
$haystack2 = $match[1];
echo '2. ' . $haystack2 * 2; echo "<br />'n";
$haystack3 = preg_replace( '/[^'d'.]+/', '', $haystack); 
echo '3. ' . $haystack3 * 2; echo "<br />'n";

编辑:这是实际字符串:

string(222) "</CurrencyCode><MonetaryValue>33.35</MonetaryValue></TotalCharges><GuaranteedDaysToDelivery/><ScheduledDeliveryTime/><RatedPackage><TransportationCharges><CurrencyCode>"

因此,我们将其放入$haystack中,并去掉XML标记:

$haystack = "</CurrencyCode><MonetaryValue>33.35</MonetaryValue></TotalCharges><GuaranteedDaysToDelivery/><ScheduledDeliveryTime/><RatedPackage><TransportationCharges><CurrencyCode>";
$haystack = strip_tags( $haystack);
var_dump( $haystack);

我们得到了输出:

 string(5) "33.35"

现在,我们可以把它乘以2,得到:

 float(66.7)

问题解决了!

我刚刚测试过,工作正常,但会丢失十进制值:

<?php
$handling = '12.24';
echo intval($handling) * 2; // prints 24
echo floatval($handling) * 2; // prints 24.48
?>

要解决您的问题,您应该使用floatval()函数包装$handling:

<?php function ups($dest_zip,$service,$weight,$length,$width,$height)
    </RatingServiceSelectionRequest>";
    $ch = curl_init("https://www.ups.com/ups.app/xml/Rate");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_TIMEOUT, 60);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    $result=curl_exec ($ch);        
    $upsout = (explode('USD', $result, 15));    
    $handling = floatval($upsout[3]);  // <--- recommend wrap here if you need to use variable later in math
    echo "<br><br>Shipping: $";
    echo $handling;
    echo " USD<br><br>";       
    curl_close($ch);
    // print_r ($upsout);}
 ?>

编辑:仔细想想,你确定你的带有$test的示例代码真的是这样吗?类型杂耍应该允许您将字符串乘以一个数字并返回一个数字。如果您将样本代码的测试行更改为:,会发生什么

print "handling is $handling 'n calculated value is $test"; 

原件:由于这似乎是您从UPS收回的成本,它很可能不是整数,因为会有美分(您提到的值的形式为00.00000.00似乎也证实了这一点)。

正因为如此,你会想把它变成一个浮动,使用以下方法之一:

$test = (float)$handling * 2;
$test2 = floatval($handling) * 2;