Jump to content

[SOLVED] functions that return arrays


jcampbell1

Recommended Posts

why doesn't this work? is there a workaround with out doing an  intermediate assignment?

 

<?php

 

echo get_dog()['legs'];

 

function get_dog() {

  return array('legs'=>4,'tails'=>1,'ears'=>2);

}

 

?>

 

What exactly are you trying to do because this function makes no sense. You seem to be telling it to retrieve a single item from the function array (which you did incorrectly anyway) and then your function returns the entire array.  If the function is going to return the entire array then why pass in legs? 

 

The previous poster got you going on the function layout as yours is no where close to correct, but you might want to share a little more info as what you're doing doesn't make a lot of sense.

 

good luck,

 

 

Link to comment
Share on other sites

that uses an intermediate assignment...

 

consider this....

 

<?php

 

echo get_dog()['legs'];  //fails

echo get_dog2()->legs;  // works

 

function get_dog() {

  return array('legs'=>4,'tails'=>1,'ears'=>2);

}

 

class dog {}

 

function get_dog2() {

  $dog = new dog;

  $dog->legs = 4;

  $dog->tails = 1;

  $dog->ears =2;

 

  return $dog;

 

}

?>

 

Link to comment
Share on other sites

I just don't understand why a function that returns an array doesn't act like an array.  It works fine in python/javascript... see:

 

<script type="text/javascript">

function get_dog() {

  a = new Array();

  a[0] = 'hello';

  a[1] = 'world';

  return a;

}

function alert_message() {

  alert(get_dog()[0] + get_dog()[1]);

}

alert_message();

</script>

Link to comment
Share on other sites

Ehh, there's a good and bad side to this lack of a feature in PHP.  The bad side is that it makes your scripts slightly longer.  Many people are used to the quick javascript chain ability where you can have anything such as:

document.getElementById('myElement').setAttribute('src', 'newsrc');

Whereas in PHP, you'd have to run the function, store it's result, and then execute the next function.  (Unless in an object context as stated in the previous post.)

 

The good side is that...well...most people don't make a function return values that they don't need, so for example if you have this function:

function get_dog() {
     return array('legs' => 4, 'tails' => 1, 'ears' => 2);
}

And you call

get_dog()['legs']

There is a pretty good chance that at some point in your script you'll need to access 'tails' and 'ears' as well, but you can't...at least...not without executing the function again, which takes more CPU cycles.  It's actually much smarter and faster, though it creates longer code, to cache results of a function to a variable which can then be used at your disposal for the rest of the script with minimal CPU effort.

Link to comment
Share on other sites

I don't think we understand what you are trying to accomplish doing this.

 

when you run the function get_dog, then the function returns the array to the value you set to equal the function, the function itself doesn't become an array.

Link to comment
Share on other sites

I don't think we understand what you are trying to accomplish doing this.

 

when you run the function get_dog, then the function returns the array to the value you set to equal the function, the function itself doesn't become an array.

I do understand where he's coming from.  He's in Javascript/C/VB/every other language that isn't PHP mode where chainability is possible.  Take the jQuery javascript library for example.  You can execute function after function just like this:

$('#myobject').addClass('newclassName').fadeIn('slow').fadeOut('slow').show('slow');

Because 1) the language supports these constructs, and 2) the library supports these constructs.

 

PHP doesn't support chaining constructs (at least with arrays).

Link to comment
Share on other sites

I don't even understand your even needing a function.  If you're end goal is to have the function do nothing but act as an array then just create an array and forget the function...  Your example is not a very clear example to accomplish anything useful so not sure what else to tell you.

 

 

Link to comment
Share on other sites

I am not really trying to accomplish anything except understand the language a little better.  I am just surprised that the -> operator can be applied to the result of a function, but the [] operator cannot be.  But here is a case where you have to use list, which I think is a syntacticly gross:

 

I would like

$value = mysql_fetch_row(mysql_qyery('SELECT MAX(id) FROM table'))[0];

instead of:

list($value) = mysql_fetch_row(mysql_query('SELECT MAX(id) FROM table'));

 

I also think it is better to avoid having intermediate values hanging around, because you can't garbage collect with a bunch of intermediate variables lying around in scope.

Link to comment
Share on other sites

Well, first off, if you're worried about variables hanging around you don't quite understand php.  Because it is only running on a page by page basis there really is not much need in garbage collecting.  javascript is the same thing.  You aren't really running a compiled application that stays up in memory all day.  Secondly, your example for the query only really holds true if you're really getting a single record or a single field.  Otherwise you are just reading the row into an array and accessing it through the normal array methods.

 

The -> method is really nothing more than a class access method and I guess a function might classify in php terms as a type of class even though its not being defined that way.  I would agree that is a bit strange to me why that would work at all.

 

I do not agree with your code being easier accessing the functions return value the way javascript does.  Its not quite as obvious what is going on when you start trying to debug stuff.  I prefer getting the return variable back and then doing with it what I want.  No other programming language works the way javascript does with respect to getting those arrays back, or at least no major language. 

 

Anyway, we're arguing about something that is pointless.  PHP does not work like javascript.  so if you want to use php, use it.  You could always go to ASP if you don't like it... :-)

 

 

 

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.