2

this question is just for fun and maybe learning a PHP shorthand trick (if exists) Let's assume that I have this code:

$item = $this->get_item($id);
$item = $this->prepare_items(array($item));
$item = $item[0];

By default, *prepare_items* function takes an array of items and return them with some modifications so if I want to call it on one item only I should push it into an array like above, but then I have to pull out the item from the array I created.

So is there a shorthand way to do this, like:

$item = $this->_prepare_items_output(array($item))[0];
// OR
$item = ($item = $this->_prepare_items_output(array($item)))[0];

Also if you have a link for a set of tips and tricks for PHP that would be great.

2
  • Is there any particular reason PHP doesn't support inline array index upon return value like this? I don't know anything about compiler design, but it seems that plenty of other languages I have used support this. Commented Nov 26, 2010 at 16:13
  • 1
    I suspect a combination of initial laziness, followed by syntax rules that had to remain silly or break too much existing code (because that's a syntax error, not a semantic issue). Commented Nov 26, 2010 at 16:16

5 Answers 5

5

You can use reset($array) to reset the internal array position and return the value of the first element.

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

5 Comments

It's not very intuitive (if I would see that in the code, it would take me half a minute to understand what the hell it is for) but a valid answer.
this actually would do the job for this particular case, I'm gonna leave it for a day and then accept your answer as this question is meant for fun and not actually a problem.
I'm certainly not advocating its use in production code, but it's a nice curiosity ;-)
Very interesting... Yes, I will never do this, but one of those dark corners of PHP I've never seen. Interesting...
Yeah, according to the comments section in the documentation this would give unexpected results on associative arrays:php.net/manual/en/function.reset.php#96090 But still it works for this case :-)
2

Nope, as far as I know, there is no way to do this in PHP.

What you could do is return an object of a class that has a method getLine(). With that, you could do

$item = $this->prepare_items(array($item))->getLine(0);

you could - I'm not saying it's necessarily always a good idea, but it's becoming more and more popular, probably influenced by jQuery's elegance - also store the results of get_item in the object, and have it return $this to allow for method chaining like so:

$item = $this->get_item($id)->prepare_items()->getLine(0);

4 Comments

I hate that about PHP. It doesn't seem like a particularly difficult thing to support...
ah thanks Pekka, that was fast! LOL OK that is not necessary, by the way I have updated the question (last line).
hmm, nice addition..can you elaborate please..I'm using CI and the code is not actually like that, it's a model call:$this->Eb_model->get_item($id); which is returning $query->row_array();
@ifaour mmm, if this is following CI's coding standards, I wouldn't change it. For method chaining, every method would have to return its parent object (return $this;)
2

In PHP 5.4 this becomes an easy one-liner

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.

$firstElement = getArray()[0];

where

function getArray() {
    return array(1, 2, 3);
}

Taken from a slightly modified version of Example #7 from the PHP manual pages (Arrays).

Comments

0

For the first one, you can do this trick... for other indices, it gets uglier.

list($algo) = explode(':', $password);

I was looking for a more elegant answer but here we are :)

Comments

0
[$item] = getArray();

This will assign to $item just the first value of the array. Similarly

[$a, $b, $c] = getArray();

will assign to $a the first value, to $b the second one, etc.

Note that this will trigger an error if the array returned by the function contains an insufficient number of items to match all the required ones.

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.