Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange Array Result

Status
Not open for further replies.

laurin1

MIS
Apr 28, 2003
77
US
Code:
$aReturn[$i] = $aSQL['firstname'].' '.$aSQL['lastname'].(($aSQL['credentials'] != '') ? ', ' : '').$aSQL['credentials'];
$aReturn[$i][0]	= $aSQL['id'];
$aReturn[$i][1]	= $aSQL['firstname'];
$aReturn[$i][2]	= $aSQL['lastname'];
$aReturn[$i][3]	= $aSQL['credentials'];

returns:

Array ( [0] => 2DSPie Schulze, PT [1] => 3JKPce Keeley, PT [2] => 4MHPha Holmes, PT [3] => 5SSPn Swatek, PT [4] => 6YPPli Perez, PT )

When I expected it to return:

Array ( [0] => Debbie Schulze, PT [1] => Janice Keeley, PT [2] => Marsha Holmes, PT [3] => Susan Swatek, PT [4] => Yoheli Perez, PT )

Plus, 4 other dimensions of the array containing the other data.

It looks like it's putting the first letter of each of those variables into character location [0], [1], [2], [3] in the first dimension.

What the heck is that?

Keith Davis
MCSA, A+, N+, Guru+, Geek+, Child of God++++++
Love and Service
 
in the first line you are declaring the array element $aReturn[0] as a string.
in the next line you are trying to redeclare the string as an array. the best you can hope for is that the first line is ignored/overriden but in fact, php has a neater answer.

because you have declared the base variable as a string, php assumes that use of the [] (or {}) syntax means that you are trying to address particular positions within the string. since a position can only be one character long, php is also ignoring the subsequent characters of the assignments.

this is all documented in the manual
String access and modification by character

Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array-brackets like $str[42] so think of a string as an array of characters.

 
because you have declared the base variable as a string, php assumes that use of the [] (or {}) syntax means that you are trying to address particular positions within the string. since a position can only be one character long, php is also ignoring the subsequent characters of the assignments.

I understand that, and I've seen it in other places (like Informix SQL), but I've never seen that functionality in any PHP documentation. Have you?

What I need is to someone modify some code that normally assigns a string to an array like this:

$a[] = 'test'

So that other 'properties' are stored with it as well.

Any ideas?

Keith Davis
MCSA, A+, N+, Guru+, Geek+, Child of God++++++
Love and Service
 
yes - i have seen it in php documentation. I posted about this a couple of weeks ago, and the quote from above is from the manual.

as for the issue at hand i think that you would be better off doing something like this

Code:
$aReturn[$i]['salutation'] = $aSQL['firstname'].' '.$aSQL['lastname'].(($aSQL['credentials'] != '') ? ', ' : '').$aSQL['credentials'];
$aReturn[$i][id]    = $aSQL['id'];
$aReturn[$i]['firstname']    = $aSQL['firstname'];
$aReturn[$i]['lastname']    = $aSQL['lastname'];
$aReturn[$i]['credentials']    = $aSQL['credentials'];
and have a numeric array of associative array.
 
Oh, now I see it in the manual. I can't change the way the original behaves. I've considered what you are suggesting, but it means re-write of a large amount of code and the default behavior of the function changes (it returns a simple array that can be passed as a array to other functions, as an argument.)

Keith Davis
MCSA, A+, N+, Guru+, Geek+, Child of God++++++
Love and Service
 
i think you have limited options, if changing the core behaviour will break other parts.

perhaps set up a parallel array with the same base index?
 
save the parallel array as a session array? or a global variable?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top