Farer
Farer

Reputation: 25

Illegal string offset 'ERROR' on line 33

When I run this code I found on Perfect money website, it caused

PHP Warning: Illegal string offset 'ERROR' in line 33

can you please someone explain me why? - btw. Line 33 is: $ar[$key]=$item[2];

Here is the code:

<?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>';



?>

I am running this on PHP 7.1

Upvotes: 0

Views: 851

Answers (1)

ThomasVdBerge
ThomasVdBerge

Reputation: 8140

In your code, you are first declaring $ar as a string:

$ar="";

Then you are using it as an array:

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

You could resolve this warning by changing

$ar="";

to one of the following:

$ar=[]; // Short array declaration

$ar = array(); // Long array syntax declaration

 // Remove the line altogether, PHP can handle this.

Upvotes: 1

Related Questions