0

PHP:

I have made up a function that returns an array. I would like to be able to echo only one part of this array without having to make a variable like this:

$var = meth_0('getPlayerLimit');
echo $var['success'];

Is this possible? I´ve already tested doing what's bellow and it didn´t work.

echo meth_0('getPlayerLimit')['success'];

Thanks for your time :)

4
  • no this is not possible, you can use print_r() for verification Commented Jun 20, 2012 at 17:31
  • 2
    @MoyedAnsari False, see answers below, it is entirely possible ;) Commented Jun 20, 2012 at 17:35
  • @NishuTayal 'meth_0' is the name of the function. Commented Jun 20, 2012 at 17:41
  • I´ve seen both answers. I understand now. I couldn´t make it work because i´m using PHP Version 5.3.8. I will update :) Thanks Commented Jun 20, 2012 at 17:43

2 Answers 2

4

It is possible only with php 5.4 and this is called Array Dereferencing, newly added to php.

On another note, you have ways with php 5.3 and less to do this such as:

echo reset(meth_0('getPlayerLimit')); //Success must be first item
echo end(meth_0('getPlayerLimit')); //Success must be last item

or

echo valueOf(meth_0('getPlayerLimit'), 'success');
function valueOf($arr, $idx){ return $arr[$idx]; }

Thats all i can think of!

Sign up to request clarification or add additional context in comments.

Comments

3

You can do it with PHP 5.4, it is called array dereferencing:

  function get_cars()
  {
    return ["ferrari","lamborghini","maserati","bugatti"];
  }
  echo get_cars()[3];  //bugatti

example taken at http://www.waynemay.com/function-array-dereferencing-in-php-5-4-03052012

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.