cgm225 Posted August 22, 2008 Share Posted August 22, 2008 I have a method that returns a two dimensional array, from which I want to set a variable to ONLY the first array contained in the two dimensional array. Currently, I am doing so successfully like so... $this->photoData = $this->getSlicedData($this->photoPosition, 1); $this->photoData = $this->photoData[0]; HOWEVER, I would like to do this all in one line, but am having problems with the syntax... I want to do something like... $this->photoData = $this->getSlicedData($this->photoPosition, 1)[0]; ...but the [ 0 ] I tagged on there is giving me errors... Any ideas? Thanks again! Link to comment https://forums.phpfreaks.com/topic/120941-syntax-with-returned-array-from-class-method/ Share on other sites More sharing options...
JasonLewis Posted August 23, 2008 Share Posted August 23, 2008 It would. Because $this->getSliceData() isn't an array. Your setting $this->photoData as the array so you need to do it on seperate lines. Is it that big of a deal to have 1 extra line? Link to comment https://forums.phpfreaks.com/topic/120941-syntax-with-returned-array-from-class-method/#findComment-623440 Share on other sites More sharing options...
DarkWater Posted August 23, 2008 Share Posted August 23, 2008 For some reason, PHP doesn't allow that syntax (yet. Hopefully they will soon. I'd love to see it). You could always do something like: $this->photoData = array_shift($this->getSlicedData($this->photoPosition, 1)); Link to comment https://forums.phpfreaks.com/topic/120941-syntax-with-returned-array-from-class-method/#findComment-623463 Share on other sites More sharing options...
JasonLewis Posted August 23, 2008 Share Posted August 23, 2008 $this->photoData = $this->getSlicedData($this->photoPosition, 1)[0]; That syntax doesn't make sense. Not to me anyway. Link to comment https://forums.phpfreaks.com/topic/120941-syntax-with-returned-array-from-class-method/#findComment-623483 Share on other sites More sharing options...
DarkWater Posted August 23, 2008 Share Posted August 23, 2008 $this->photoData = $this->getSlicedData($this->photoPosition, 1)[0]; That syntax doesn't make sense. Not to me anyway. It makes as much sense as: <?php class Test { protected $other; public function __construct(Other $other) { $this->other = $other; } public function getOther() { return $this->other; } } class Other { public function test() { echo "Testing"; } } $other = new Other(); $test = new Test($other); $test->getOther()->test(); ?> Referring to the last line of that, of course. Link to comment https://forums.phpfreaks.com/topic/120941-syntax-with-returned-array-from-class-method/#findComment-623489 Share on other sites More sharing options...
JasonLewis Posted August 23, 2008 Share Posted August 23, 2008 No that makes sense, lol. Because $test->getOther() is an object already. In the OP's example $this->getSlicedData($this->photoPosition, 1) is just calling the function. It isn't an array yet. Which is why this syntax: $this->getSlicedData($this->photoPosition, 1)[0] Makes no sense. Link to comment https://forums.phpfreaks.com/topic/120941-syntax-with-returned-array-from-class-method/#findComment-623505 Share on other sites More sharing options...
DarkWater Posted August 23, 2008 Share Posted August 23, 2008 The return of the function is an array though, just like the return $this->getOther() is an Other object. Link to comment https://forums.phpfreaks.com/topic/120941-syntax-with-returned-array-from-class-method/#findComment-623511 Share on other sites More sharing options...
JasonLewis Posted August 23, 2008 Share Posted August 23, 2008 I think I can see where you are coming from. But still, to me... Your example makes sense, the OP's one doesn't. Link to comment https://forums.phpfreaks.com/topic/120941-syntax-with-returned-array-from-class-method/#findComment-623528 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.