Welcome to WebmasterWorld Guest from 54.227.14.23

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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 16, 2002
posts:2010
votes: 0


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 July 5, 2004 (gmt 0)

Administrator

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

joined:July 31, 2003
posts:12541
votes: 1


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 July 5, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 16, 2002
posts:2010
votes: 0


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 July 5, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:June 3, 2004
posts:55
votes: 0



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

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members