homepage Welcome to WebmasterWorld Guest from 54.226.43.155
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
temporary arrays as inline lists for selection
is there a shorthand I can use to help code flow
amznVibe




msg:1272499
 9:02 am on Jul 5, 2004 (gmt 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!

 

coopster




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

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)));

amznVibe




msg:1272501
 1:36 pm on Jul 5, 2004 (gmt 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.

m_shroom




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


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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved