By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,985 Members | 1,712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,985 IT Pros & Developers. It's quick & easy.

ugly code!

P: n/a
<?php
function ugly_array() { return array(1, 2, 3, 4, 5); }
$arr = ugly_array();
echo $arr[2];
?>

not so ugly :)
now ... I want to get rid of the $arr temporary variable.

attempts (no more <?php and ?>; one attempt per line)

echo ugly_array()[2]; ## parse error

echo (ugly_array())[2]; ## parse error!

echo ((ugly_array())[2]); ## parse error

echo ({ugly_array()}[2]); ## parse error

echo ugly_array()([2]); ## parse error

echo +(ugly_array())[2]; ## parse error!!!!

echo '-'.(ugly_array())[2]; ## parse error
and finally ... the ugly code that works

echo implode('', array_slice(ugly_array(), 2, 1));

What am I missing here?
Can't I get an element of an array returned from a function?

What's so different about the array being named or unnamed?

--
..sig
Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
I wrote:
<?php
function ugly_array() { return array(1, 2, 3, 4, 5); } What am I missing here?
Can't I get an element of an array returned from a function?

What's so different about the array being named or unnamed?
tested on Linux:
$ php -v
PHP 4.3.3 (cli) (built: Nov 19 2003 23:12:29)
Copyright (c) 1997-2003 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2003 Zend Technologies

tested on Windows: cli\php -v

PHP 5.0.0-dev (cli) (built: Feb 13 2003 00:14:28)
Copyright (c) 1997-2003 The PHP Group
Zend Engine v2.0.0-dev, Copyright (c) 1998-2003 Zend Technologies

--
..sig
Jul 17 '05 #2

P: n/a
Pedro Graca <he****@hotpop.com> wrote in message news:<bp*************@ID-203069.news.uni-berlin.de>...
<?php
function ugly_array() { return array(1, 2, 3, 4, 5); }
$arr = ugly_array();
echo $arr[2];
?>

not so ugly :)
now ... I want to get rid of the $arr temporary variable.


IMO, you can't.

---
"Dying is an art, like everything else"---Sylvia Plath
Email: rrjanbiah-at-Y!com
Jul 17 '05 #3

P: n/a
Pedro Graca wrote:
<?php
function ugly_array() { return array(1, 2, 3, 4, 5); }
$arr = ugly_array();
echo $arr[2];


not so ugly :)
now ... I want to get rid of the $arr temporary variable.

attempts (no more <?php and ?>; one attempt per line)


snip

how about modifying your function to better suite your needs:

function ugly_array($i = "none") {
$tmparr = array(1, 2, 3, 4, 5);
if ($i == "none") { return $tmparr; }
else { return $tmparr[$i]; }
}

This way the array works just as before if no parameters are given. But
you can also use it like this:

echo ugly_array(2);

HTH

--
Suni

Jul 17 '05 #4

P: n/a
Juha Suni wrote:
[...]
how about modifying your function to better suite your needs:

function ugly_array($i = "none") {
$tmparr = array(1, 2, 3, 4, 5);
if ($i == "none") { return $tmparr; }
else { return $tmparr[$i]; }
}

This way the array works just as before if no parameters are given. But
you can also use it like this:

echo ugly_array(2);


Nice idea :)
Thing is I can't change the function that returns the array (my problem
showed up when using explode()), byt I will try a wrapper:

function my_explode($separator, $string, $limit=0, $i=-1) {
if ($limit) {
$tmparray = explode($separator, $string, $limit);
} else {
$tmparray = explode($separator, $string);
}
if ($i>=0) {
if ($i<count($tmparray)) return $tmparray[$i];
else return NULL;
} else {
return $tmparray;
}
}

--
--= my mail address only accepts =--
--= Content-Type: text/plain =--
Jul 17 '05 #5

P: n/a
A tiny bit less ugly:

echo array_pop(array_slice(ugly_array(), 2, 1));

Pedro Graca <he****@hotpop.com> wrote in message news:<bp*************@ID-203069.news.uni-berlin.de>...
<?php
function ugly_array() { return array(1, 2, 3, 4, 5); }
$arr = ugly_array();
echo $arr[2];
?>

not so ugly :)
now ... I want to get rid of the $arr temporary variable.

attempts (no more <?php and ?>; one attempt per line)

echo ugly_array()[2]; ## parse error

echo (ugly_array())[2]; ## parse error!

echo ((ugly_array())[2]); ## parse error

echo ({ugly_array()}[2]); ## parse error

echo ugly_array()([2]); ## parse error

echo +(ugly_array())[2]; ## parse error!!!!

echo '-'.(ugly_array())[2]; ## parse error
and finally ... the ugly code that works

echo implode('', array_slice(ugly_array(), 2, 1));

What am I missing here?
Can't I get an element of an array returned from a function?

What's so different about the array being named or unnamed?

Jul 17 '05 #6

P: n/a
I often encounter a similar problem:
A function returns an array and I just need one element from it. It seems
illogical to me that I cannot simply write each($a)[0]. I tried to use {...}
but it doesn't work. Instead I am forced to create a variable to store the
result of each() and then take my element in a separate instruction. Is
there really no way to do this in one step?

Thomas
Jul 17 '05 #7

P: n/a
Pedro Graca wrote:
<?php
function ugly_array() { return array(1, 2, 3, 4, 5); }
$arr = ugly_array();
echo $arr[2];
?>

not so ugly :)
now ... I want to get rid of the $arr temporary variable.

[snip]

Why not just have your own index function:

function index($arr,$i) { return $arr[$i]; }

(probably with some bounds checking though)

then use:

echo index(ugly_array(),2);

Robin

Jul 17 '05 #8

P: n/a
Robin Goodall wrote:
Why not just have your own index function:

function index($arr,$i) { return $arr[$i]; }

(probably with some bounds checking though)

then use:

echo index(ugly_array(),2);


Great! Thank you Robin

<?php
function index(&$arr, $i) { return $arr[$i]; }
function ugly_array() { return array(1, 2, 3, 4, 5); }

echo index(ugly_array(), 2);
// not as "beautiful" as ugly_array()[2] but nearly so :)
?>

--
--= my mail address only accepts =--
--= Content-Type: text/plain =--
Jul 17 '05 #9

P: n/a
Robin Goodall wrote:

Pedro Graca wrote:
<?php
function ugly_array() { return array(1, 2, 3, 4, 5); }
$arr = ugly_array();
echo $arr[2];
?>

not so ugly :)
now ... I want to get rid of the $arr temporary variable.

[snip]

Why not just have your own index function:

function index($arr,$i) { return $arr[$i]; }

(probably with some bounds checking though)

then use:

echo index(ugly_array(),2);


That's pretty. Never though of it.

Thanks,
Shawn

--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.