xaeryan Posted March 6, 2008 Share Posted March 6, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/94735-help-shortcutting-array-syntax/ Share on other sites More sharing options...
Psycho Posted March 6, 2008 Share Posted March 6, 2008 I don't think you can get it to work in that manner. You could change the function to return only the value you want to print to the page. Quote Link to comment https://forums.phpfreaks.com/topic/94735-help-shortcutting-array-syntax/#findComment-485019 Share on other sites More sharing options...
xaeryan Posted March 6, 2008 Author Share Posted March 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/94735-help-shortcutting-array-syntax/#findComment-485125 Share on other sites More sharing options...
Psycho Posted March 6, 2008 Share Posted March 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/94735-help-shortcutting-array-syntax/#findComment-485328 Share on other sites More sharing options...
kenrbnsn Posted March 6, 2008 Share Posted March 6, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/94735-help-shortcutting-array-syntax/#findComment-485343 Share on other sites More sharing options...
xaeryan Posted March 12, 2008 Author Share Posted March 12, 2008 Thanks guys!! Quote Link to comment https://forums.phpfreaks.com/topic/94735-help-shortcutting-array-syntax/#findComment-490736 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.