Avatar

is there a way to do this is one step

1
2
$clients = $CLIENT->find($options);
$client = $clients[0];

Refactorings

No refactoring yet !

330555fba743b009765f9d38d7137e6f

Scott Reynen

February 23, 2009, February 23, 2009 20:50, permalink

No rating. Login to rate!

See: http://www.php.net/list

Also, it's a good idea to use a different variable name for the two different $client variables.

1
list( $client ) = $CLIENT->find($options);
Avatar

anonymous

February 26, 2009, February 26, 2009 20:09, permalink

No rating. Login to rate!

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?

1bf59120892e34eb60836725566c6e55

fain182.myopenid.com

March 2, 2009, March 02, 2009 08:23, permalink

No rating. Login to rate!

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
Ade07afe7c1ec57379d1aa885dc1da00

Alejandro Rivera

March 13, 2009, March 13, 2009 06:30, permalink

No rating. Login to rate!
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
Avatar

Dan

May 5, 2009, May 05, 2009 20:04, permalink

No rating. Login to rate!

Why can't he just do
$client = ($CLIENT->find($options))[0];

Avatar

Dan

May 5, 2009, May 05, 2009 20:04, permalink

No rating. Login to rate!

Why can't he just do
$client = ($CLIENT->find($options))[0];

Avatar

Alix Axel

June 16, 2009, June 16, 2009 00:37, permalink

No rating. Login to rate!

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...');

?>
Avatar

Alix Axel

June 16, 2009, June 16, 2009 00:39, permalink

1 rating. Login to rate!

Or you can also use array_slice().

1
2
3
4
5
<?php

array_slice($CLIENT->find($options), 42, 1);

?>
Avatar

tight

July 24, 2009, July 24, 2009 14:49, permalink

No rating. Login to rate!

It's on PHP 6's roadmap (http://wiki.php.net/summits/pdmnotesmay09)

Your refactoring





Format Copy from initial code

or Cancel