1 2
$clients = $CLIENT->find($options); $client = $clients[0];
Refactorings
No refactoring yet !
Scott Reynen
February 23, 2009, February 23, 2009 20:50, permalink
Also, it's a good idea to use a different variable name for the two different $client variables.
1
list( $client ) = $CLIENT->find($options);
anonymous
February 26, 2009, February 26, 2009 20:09, permalink
Yea but that only works for this case because you are indexing 0. Suppose you want to get index 42, then it's obviously impractical to list so many items explicitly. What would you do?
fain182.myopenid.com
March 2, 2009, March 02, 2009 08:23, permalink
you can use end(), if you want take the last element
1 2 3 4
http://it2.php.net/manual/en/function.end.php
$fruits = array('apple', 'banana', 'cranberry');
echo end($fruits); // cranberry
Alejandro Rivera
March 13, 2009, March 13, 2009 06:30, permalink
1 2 3 4
$client = array_shift( $CLIENT->find($options) ); "array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be touched." http://www.php.net/manual/en/function.array-shift.php
Dan
May 5, 2009, May 05, 2009 20:04, permalink
Why can't he just do
$client = ($CLIENT->find($options))[0];
Dan
May 5, 2009, May 05, 2009 20:04, permalink
Why can't he just do
$client = ($CLIENT->find($options))[0];
Alix Axel
June 16, 2009, June 16, 2009 00:37, permalink
You can simplify the Value() function quite a bit if you want to.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
<?php function Value($array, $key = 0, $default = false) { if (is_array($array) === true) { if (is_array($key) === true) { foreach ($key as $value) { if (array_key_exists($value, $array) === true) { $array = $array[$value]; } else { return $default; } } return $array; } else if (array_key_exists($key, $array) === true) { return $array[$key]; } } return $default; } Value($CLIENT->find($options), 0); // ... Value($CLIENT->find($options), 42, 'what to return in case index 42 is not here...'); ?>
Alix Axel
June 16, 2009, June 16, 2009 00:39, permalink
Or you can also use array_slice().
1 2 3 4 5
<?php array_slice($CLIENT->find($options), 42, 1); ?>
tight
July 24, 2009, July 24, 2009 14:49, permalink
It's on PHP 6's roadmap (http://wiki.php.net/summits/pdmnotesmay09)
is there a way to do this is one step