RE: [PHP] Array vs. Scalar Lists
| From: | Anthony Martin | Date: | Fri, 01 Dec 2000 20:53:00 +0000 |
| Subject: | RE: [PHP] Array vs. Scalar Lists | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-28287@lists.php.net to get a copy of this message | ||
"ibase_execute( $prep, implode('',$data))" is the same as "ibase_execute (
$prep, $data[0] . $data[1] )" . It's still one value, but now it's
concatenated.
Here's all of the examples I could think of. They used echo, for
simplicity:
<?
header ( "Content-type: text/plain" ) ;
$string1 = 'Hello' ;
$dlm = ' ' ;
$string2 = 'world' ;
$punct = '!' ;
$strings = Array ( $string1, $dlm, $string2, $punct ) ;
// works as advertised, but it's hard coded
echo $string1, $dlm, $string2, $punct ;
echo chr(13) ;
// same as above
echo $strings[0], $strings[1], $strings[2], $strings[3] ;
echo chr(13) ;
// does not work as above
echo $strings ;
echo chr(13) ;
// here we go, oh guess not
echo array_slice ( $strings, 0, count ( $strings ) ) ;
echo chr(13) ;
// yuck - it's just one long string now
echo implode ( '', $strings ) ;
echo chr(13) ;
// works, but echo issued multiple times
foreach ( $strings as $val ) {
echo $val ;
}
echo chr(13) ;
// works great, but too complicated
$stmt = 'echo ' ;
foreach ( $strings as $key => $val ) {
if ( next ( $strings ) ) {
$stmt .= "\$strings[$key], " ;
} else {
$stmt .= "\$strings[$key] ;" ;
}
}
eval ( $stmt ) ;
?>
Anthony
-----Original Message-----
From: James Moore [mailto:jmoore@php.net]
Sent: Friday, December 01, 2000 12:56 PM
To: Anthony Martin; 'Lars Torben Wilson'
Cc: php-general@lists.php.net
Subject: RE: [PHP] Array vs. Scalar Lists
Whats wrong with somthing like ibase_execute( $prep, implode('',$data)); ?
James
-----Original Message-----
From: Anthony Martin [mailto:Anthony@TRAMS.com]
Sent: 01 December 2000 20:41
To: 'Lars Torben Wilson'
Cc: 'php-general@lists.php.net'
Subject: RE: [PHP] Array vs. Scalar Lists
Ok, like I said, I'm using echo as an example. What I'm really trying to do
is a little more complicated:
$data = Array ( 'c1f2g3h4j5r', 'AC', ) ;
$stmt = 'SELECT * FROM PROFILE WHERE SESSION = ? AND STATUS = ? ;' ;
$prep = ibase_prepare ( $dbh, $stmt ) ;
$result = ibase_execute ( $prep, $data[0], $data[1] ) ;
I want to avoid having to pass each value of the array to ibase_execute. I
would much rather do:
$result = ibase_execute( $prep, $data ) ;
But this function wants to see two values. It doesn't take into account
that it's been given an array with two values in it. So if I want a
programmatic solution, I have to use my eval() insanity (or is that evil
insanity like Microsoft's spell check keeps telling me it is).
Anthony
-----Original Message-----
From: Lars Torben Wilson [mailto:torben@pinc.com]
Sent: Friday, December 01, 2000 8:28 PM
To: Anthony Martin
Cc: 'php-general@lists.php.net'
Subject: Re: [PHP] Array vs. Scalar Lists
Anthony Martin writes:
[trim]
> So, I want to _simulate_ the following:
>
> echo $arrayOfStrings[0], $arrayOfStrings[1], $arrayOfStrings[2] ;
>
> The problem with the above statement is that if $arrayOfStrings[3] exists,
> it's not printed.
>
> I found a way to do it with eval(), and it works to my satisfaction
because
> I'm not actually imploding the values, just the keys of the array in the
> proper syntax:
>
> $stmt = 'echo ' ;
> foreach ( $arrayOfStrings as $key => $val ) {
> if ( next ( $strings ) ) {
> $stmt .= "\$arrayOfStrings[$key], " ;
> } else {
> $stmt .= "\$arrayOfStrings[$key] ;" ;
> }
> }
> eval ( $stmt ) ;
>
> I'm sure I could have used another function like array_keys(), but it
helps
> to get the keys one at a time so I can add the correct syntax. But is
there
> a way to do this without eval()?
>
>
> Anthony
Why don't you like implode()? You're doing an insane amount of work to
accomplish pretty much the same thing...and even so, the above would
be better written as
$stmt = 'echo ' . implode(', ', $arrayofstrings) . ";";
eval($stmt);
What added value are you getting from the eval() here?
--
+----------------------------------------------------------------+
|Torben Wilson <torben@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 torben@netmill.fi|
+----------------------------------------------------------------+
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net