Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8733535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:40:04+00:00 2026-06-13T09:40:04+00:00

Given a function which returns an array; function return_me_an_array() { return array(‘my_value’); } How

  • 0

Given a function which returns an array;

function return_me_an_array() {
  return array('my_value');
}

How can I call it and get a single value (whether, first, last or only) from the array in an elegant fashion (yes I know this is PHP!)

If I do this

$var = reset(return_me_an_array());

I get the following PHP strict error:

Strict warning: Only variables should be passed by reference

Likewise each of these give the same warning.

$var = array_shift(return_me_an_array());
$var = array_pop(return_me_an_array());

I know I can do this:

$temp = return_me_an_array();
$var = $reset($temp);

But having to do it over two statements is pretty horrible.

Is there a good way to do this?

On the face of it this is similar to How to return an array and get the first element of it in one line in PHP? but the accepted answer uses reset – so I think this is different; I’m either looking for a solution that doesn’t give strict warnings, or to be told what I want is impossible.

I’m also aware the PHP 5.4 has introduced array dereferencing (e.g. return_me_an_array()[0]) but alas I am currently using 5.3.

Edit after being closed: How is this too localized? This is a general question about how to write elegant PHP. Or is it the case that so few people care about elegant PHP it is “an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:40:05+00:00Added an answer on June 13, 2026 at 9:40 am

    Use this:

    $var = current(return_me_an_array());
    

    Demo: http://3v4l.org/Ruj5e

    Manual: http://php.net/manual/en/function.current.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I write a function which will return pi (π) to a given
I want to write a function which can validate a given value (passed as
Given remote page: http://example.com/paged_list.aspx which uses a Javascript function call to display several pages
I am wondering does the size() function returns an internal stored variable (which gives
Suppose that my Haskell function is given an input, which is supposed to be
Given the following lines of code which is using JQTOUCH: $('#customers').bind('pageAnimationEnd', function(e, info){ if
Hello friends i am running code given below which contains the setLogTimeEntery function and
Can some give me good jQuery lightbox plugin which has zoomin and zoomout function
How do I return the following json data in a succesfull array: [{Identifier:1,Naam:NOT Given,Adres:Kopenhagen
I have this function which gives me ( for a current date ) -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.