开发者

Why can't I reference this array index that seems to exist?

开发者 https://www.devze.com 2023-01-06 01:30 出处:网络
I\'m getting a return string from a merchant account that looks like: RecurringID=8675309&RefNo=41:39&Notes=

I'm getting a return string from a merchant account that looks like:

RecurringID=8675309&RefNo=41:39&Notes=

so I parse it into an array like this:

$results = array();
$temp = explode('&', $temp);
foreach($temp as $line)
{
    $line = explode('=', $line);
    $results[trim($line[0])] = trim($line[1]);
}

The resulting prin开发者_如何学运维t_r($results); produces this:

Array ( [RecurringID] => 8675309 [RefNo] => 41:39 [Notes] => ) 

And yet when I try this:

$blah = $results['RecurringID'];

I get:

Notice (8) : Undefined index:  RecurringID


I can't reproduce this error. Are you using it it before the RecurringID index is defined?

Do you know about parse_str()

$temp = 'RecurringID=8675309&RefNo=41:39&Notes=';
parse_str($temp, $results);

print "<pre>";
print_r($results);
print "</pre>";

$results has this in it:

Array
(
    [RecurringID] => 8675309
    [RefNo] => 41:39
    [Notes] => 
)

This works fine:

print $results['RecurringID']; //8675309

I can't replicate your warning even if I add:

error_reporting(E_ALL);

...above everything else.


Aha! My problem was outputing it to the browser, I couldn't see the full return value. Turns out looking at the ascii values reveals:

\r\n\t\t<html><body>RecurringID=1488819&RefNo=186:192&Notes=</body></html>

Which apparently was confusing parse_str and my manual method for parsing.

0

精彩评论

暂无评论...
验证码 换一张
取 消