Jump to content

Help shortcutting array syntax


xaeryan

Recommended Posts

I have a function which returns an array.  I would like to shortcut the following code:

$myObject = thisFunctionReturnsAnArray();
print $myObject['SomeArrayRow'];

 

with something similar to the following:

print thisFunctionReturnsAnArray()['SomeArrayRow'];

 

I've experimented with the syntax and can't seem to get it straight...  any suggestions?

Link to comment
Share on other sites

Unfortunately, I need the function to return the whole array, as there are going to be some places where I request different row values.

The function gets the array from a mySQL database, so the array row values are dependent on the database, hence I can't craft separate functions to return the individual values (and plus that would require many functions since the database has many rows).

Hmm, so there's no way to do this it seems like, not even using some crazy $($result = function())['row']) syntax?  Darn.

 

Link to comment
Share on other sites

Well, i think you still need to go back to your function. I would create a function that takes an optional parameter. If the parameter is present the function would return only the values necessary from the query. If no parameter is present it would return the whole array. here is an example:

 

Let's say the array that would normally be returned from the function looked like this

array (
    'IndexA' => 'ValueA'
    'IndexB' => 'ValueB'
    'IndexC' => 'ValueC'
    'IndexD' => 'ValueD'
}

 

Ok, in "some" situations you just want an individual value, in others you need the whole thing. So, the function could look like this:

<?php
function getValues($return_index=false) {

  //Code to generate $values_array goes here

    if ($return_index!=false && isset($values_array[$return_index])) {
        return $values_array[$return_index];
    } else {
        return $values_array;
    }
}
?>

 

Now if you call

$myObject = getValues();

you get the whole array back just as you do now. But you can also just have a single value returned (and use within an echo) by calling the function like this

echo getValues('IndexC');

 

However, as stated previously I would go one step further and make it so the query is only selecting the data requested.

Link to comment
Share on other sites

If you know the location of the value in the array, you can use the list() statement:

<?php
  $test_array = array('one','two','three','four');
  list(,,,$var) = $test_array;
  echo $var;
?>

The above will echo "four".

 

In your case, if the value you wanted was the third in the array, you could do:

<?php
list(,,$val) = thisFunctionReturnsAnArray();
echo $val;
?>

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.