Array vs. Scalar Lists
| From: | Anthony Martin | Date: | Fri, 01 Dec 2000 20:12:01 +0000 |
| Subject: | Array vs. Scalar Lists | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-28275@lists.php.net to get a copy of this message | ||
I was wondering if there's a way to pass array keys implicitly to a function
that expects a list of scalars.
I'm used to perl's method of handling arrays as lists of scalars. If I
wanted to print the concatenated contents of an array, I would just pass the
entire array.
If I did this with echo, I get the word "Array" (no real surprise). Now, I
understand how to extract all of the values out of the array and pass them
as one string, like:
echo implode ( '', $arrayOfStrings ) ;
That would accomplish the task, but not the way I would like. I'm just
using echo as an example.
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