2

For example:

$fruits = array(
    1 => 'apples',
    2 => 'lemons',
    3 => 'bananas'
);

Is there a function to output lemons, without using $fruits[2]?

1
  • @codemonkey613: Why would you not want to use $fruits[2]? That is the best way to do it. Commented Mar 13, 2010 at 16:05

2 Answers 2

2

You could use next(), current(), prev(), end() set of functions. You could use a foreach on the array. You could use the list($var,$var1,$var2...) = $arr construct. Be more specific as to what you're trying to do.

EDIT:

If you're looking for a way to echo it in text use 
$foo='LEMON: '.$fruits[2].' =)';
OR
$foo=:LEMON: {$fruits[2]} =)";

foreach($fruits as $k => $v) if ($k===2) echo $v;

list($f1,$f2,$f3) = $fruits;
echo $f2;

next($fruits);
echo next($fruits);

array_shift($fruits);
echo $array_shift($fruits);
Sign up to request clarification or add additional context in comments.

1 Comment

There isn't, but you can make one if you use it often. function getValueIndex($output,$key){ return $output[$key]; } ... so then getValueIndex($class->returnsAnArray(),1);
1

array_shift():

echo array_shift($fruits);

But it works only with the first element in the array of course ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.