Welcome to WebmasterWorld Guest from 107.20.20.39

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

temporary arrays as inline lists for selection

is there a shorthand I can use to help code flow

   
9:02 am on Jul 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am wondering if it is possible to do some kind of shorthand I can use to make PHP allow a temporary array without extra code to asign a string and select it.

example: (longhand I am trying to avoid)


$fruits=array("apples","pears","oranges","bananas");
$fruitSelection=1;
echo $fruits[$fruitSelection];

is there some way to do a shorthand like this?

echo array("apples","pears","oranges","bananas")[1];

Thanks for any help!

12:57 pm on Jul 5, 2004 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



echo "pears";

hehe. Sorry, couldn't resist.

To the best of my knowledge, no, well, not without jumping through hoops first. The point I am making here is that you already know which one you want, why go through the necessary steps to build the array? OK, I guess you could argue that it is all dynamic...Well, here is one example of jumping through the hoops

echo (array_pop(array_slice(array("apples","pears","oranges","bananas"), 1, 1)));
1:36 pm on Jul 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah thanks for the attempt. Apparently there is no simple way to immediately treat an array as an element itself.

In the end I figured I'll just build a simple function to take the raw array and return the string so I don't have to repeat all that code.

3:07 pm on Jul 5, 2004 (gmt 0)

10+ Year Member




$fruits[]="apples";
$fruits[]="pears";
$fruits[]="oranges";
$fruits[]="bananas";
$n=0;
while ($n<4)
{echo $fruits[$n]; $n++; }
 

Featured Threads

Hot Threads This Week

Hot Threads This Month