tags:

views:

64

answers:

2

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 print_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
A: 

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.

artlung
Did you mean to say "I can't reproduce this error"?
Jamie Wong
I suspect he did. The code is in the logical order ( reference to RecurringID is at the bottom). I suspect cakephp is doing something crazy to my array.
Jack B Nimble
Jamie Wong - yes! Fixed.
artlung
Jack B Nimble - peculiar!
artlung
A: 

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.

Jack B Nimble
`invisible character in the return` ...or in your code. Seems like the most logical explanation. At least Cake doesn't have anything to do with this. Try `var_dump(array_flip($results))` to check the length of the keys.
deceze