tags:

views:

85

answers:

3

I have a multi-dimensional array with 4 entries in each value - (1st name, last name, email, password). I am trying to format the password value so I can insert it into a database. As you can see, my results are not what I need. Why is this the result and what should I do to get my intended result? Thanks

php > $newlines[1][3] = "PASSWORD($newlines[1][3)]";  
php > echo $newlines[1][3];  
PASSWORD(Array[3)]
+1  A: 
"PASSWORD($newlines[1][3)]"

Should be

"PASSWORD({$newlines[1][3]})"
baloo
+1  A: 

This appears to be a quotation mark placement problem. You want the result of the function Password() rather than the string "Password(-arguments-)".

Drop the quotations around the right side:

php > $newlines[1][3] = password($newlines[1][3]);
JYelton
`PASSWORD()` is probably the MySQL function.
Felix Kling
Ah, then in that case, the other answers should suffice as the transposition of bracket and parenthesis looks suspect.
JYelton
+3  A: 

You have a typo:

php > $newlines[1][3] = "PASSWORD($newlines[1][3)]";
                                                ^

But this not the only problem. You are accessing a multi-dimensional array and therefore, you have to put the array access into brackets {}. Otherwise, PHP would only subsitute the variable up to the first index (i.e. $newlines[1]). See also variable parsing.

And as $newlines[1][3] is most likely a string, you should also put quotation marks around it:

php > $newlines[1][3] = "PASSWORD('{$newlines[1][3]}')";

or even better in my opinion:

php > $newlines[1][3] = "PASSWORD('" . $newlines[1][3] . "')";
Felix Kling
Your third suggestion using concatenation did the trick. All of the others returned Array[3] when I wanted the string that was in the Array. Thank you.
aliov
@aliov: The problem was, that if you don't put `{}` around the variable, PHP will only parse the variable up to the first index (i.e. $newlines[1]) which is an array and therefore prints `Array`. You are right, even if you had no typo, you would have got the wrong result. Putting it into brackets should work, but personally I find concatenation much more readable anyway (it is easier to see, what is PHP and what not).
Felix Kling
Thanks for the mini-lesson and the sound advice. Much appreciated.
aliov