开发者

Unable to access array member - UPDATED

I have a PHP function that returns an array. This is the output of print_r($myArray):

Array ( 
[id] => 8166 
[customer_id] => 73 
[nickname] => AnnieB 
[name] => Anastasia Beaverhausen 
[email] => annieb@annieb.com 
[phone] => 555-555-5555 
[company] => Annie B's 
[address1] => 123 Main Street 
[address2] => Apartment 555 
[city] => Chicago 
[state] => IL 
[zip] => 55555 
[billing] => 1 
[residence] => 0 
[token] => 
[verified] => 1 ) 

I should be able to access any of the members by saying something like $myArray['city'], correct? I know I've done this in the past, but it keeps returning an empty string, even when there is a value in 开发者_运维技巧the city field.

Any ideas? ==================MORE CODE POSTED PER REQUESTS============================= I'm using this in Joomla, so there are a few lines that are specific to Joomla. The end purpose of this code is to populate a dropdown list with addresses from a database; the option values contain an imploded string of all column values (to be accessed via javascript later) and the option text is a single field. Here's the code that creates the dropdown:

 foreach (getAddresses($AcctID) as $id => $info) {
                print_r($info);
                $nickName = $info[2];
                error_log("nickname=".$nickName);
                $infoStr = implode("|", $info);
                $addressOptions .= "<option value=\"{$infoStr}\">$nickName</option>";
     }

The getAddresses function is here (this is working correctly):

function getAddresses($AcctID) {
    $db =& JFactory::getDBO();
    $query = "select * from jos_customers_addresses where customer_id = ".$db->quote($AcctID);
    $db->setQuery($query);
    if (!$db->query()) error_log($db->stderr());

    if (!$db->getNumRows()) 
        return false;
    else
        return $db->loadAssocList();
}

The print_r($info) line is what is returning the array I originally posted. The next two lines are the ones giving me problems:

$nickName = $info[2];
error_log("nickname=".$nickName);

I've also tried $nickName = $info['nickname'] and gotten the same result - no value, even though there's obviously a value in the print_r, and the value does come through correctly in the code generated by the implode line. If you can find someplace between those two lines where I'm overwriting my variable, or whatever, please point it out to me, because I'm clueless as to why I can't get a handle on this value.


I should be able to access any of the members by saying something like $myArray['city'], correct?

Yes, that's correct.

I know I've done this in the past, but it keeps returning an empty string, even when there is a value in the city field.

If nothing is showing up, there's probably not a value there, which as the comments said, is most likely a bug in your code (something overwriting that key). To be sure there's nothing there, instead of echo $myArray['city'] try var_dump($myArray['city']) -- this will tell you if it's an empty string (string(0) "") or not set at all (NULL)

Take your print_r($myArray) and put it immediately before the statement where you use $myArray['city']. is the 'city' key still set? If so, double check to make sure you don't have a typo... capitalization matters! If it's not still set, move the print_r($myArray) up a few lines and repeat until you find what's unsetting $myArray['city']

...Or just post more code ;-)

EDIT: Did $info[2] need to be $info['city']?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜