第 33 行上的非法字符串偏移量"错误"



当我运行我在完美货币网站上找到的这段代码时,它导致了

PHP 警告:第 33 行中的非法字符串偏移量"错误"

你能请别人解释一下为什么吗? - 顺便说一句。第 33 行是:$ar[$key]=$item[2];

这是代码:

<?php
/*
This script demonstrates transfer proccess between two
PerfectMoney accounts using PerfectMoney API interface.
*/
// trying to open URL to process PerfectMoney Spend request
$f=fopen('https://perfectmoney.is/acct/confirm.asp?AccountID=myaccount&PassPhrase=mypassword&Payer_Account=U987654&Payee_Account=U1234567&Amount=1&PAY_IN=1&PAYMENT_ID=1223', 'rb');
if($f===false){
echo 'error openning url';
}
// getting data
$out=array(); $out="";
while(!feof($f)) $out.=fgets($f);
fclose($f);
// searching for hidden fields
if(!preg_match_all("/<input name='(.*)' type='hidden' value='(.*)'>/", $out, $result, PREG_SET_ORDER)){
echo 'Ivalid output';
exit;
}
$ar="";
foreach($result as $item){
$key=$item[1];
$ar[$key]=$item[2];
}
echo '<pre>';
print_r($ar);
echo '</pre>';

?>

我在 PHP 7.1 上运行它

在代码中,首先将$ar声明为字符串:

$ar="";

然后你把它作为一个数组:

foreach($result as $item){
$key=$item[1];
$ar[$key]=$item[2];
}

您可以通过更改

$ar="";

到以下之一:

$ar=[]; // Short array declaration
$ar = array(); // Long array syntax declaration
// Remove the line altogether, PHP can handle this.

最新更新