Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better PHP way for getting default value by key from array (dictionary)?

In Python one can do:

foo = {} assert foo.get('bar', 'baz') == 'baz' 

In PHP one can go for a trinary operator as in:

$foo = array(); assert( (isset($foo['bar'])) ? $foo['bar'] : 'baz' == 'baz' ); 

I am looking for a golf version. Can I do it shorter/better in PHP?

UPDATE [March 2020]:

assert($foo['bar'] ?? 'baz' == 'baz'); 

It seems that Null coalescing operator ?? is worth checking out today.

found in the comments below (+1)

like image 584
Yauhen Yakimovich Avatar asked Jul 14 '11 16:07

Yauhen Yakimovich


People also ask

What is the main benefit of the array keyword in PHP?

Advantage of PHP ArrayLess Code: We don't need to define multiple variables. Easy to traverse: By the help of single loop, we can traverse all the elements of an array. Sorting: We can sort the elements of array.

Does PHP use a dictionary to get values from a form?

There are no dictionaries in php, but PHP array's can behave similarly to dictionaries in other languages because they have both an index and a key (in contrast to Dictionaries in other languages, which only have keys and no index).

Which array has pair of value and key in PHP?

Associative arrays - Arrays with named keys.

What is the default value of variable in PHP?

1 Answer. Show activity on this post. Variables that are declared without a value and undefined/undeclared variables are null by default.


2 Answers

Time passes and PHP is evolving. PHP 7 now supports the null coalescing operator, ??:

// Fetches the value of $_GET['user'] and returns 'nobody' // if it does not exist. $username = $_GET['user'] ?? 'nobody'; // This is equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';  // Coalescing can be chained: this will return the first // defined value out of $_GET['user'], $_POST['user'], and // 'nobody'. $username = $_GET['user'] ?? $_POST['user'] ?? 'nobody'; 
like image 170
Ivan Yarych Avatar answered Oct 05 '22 21:10

Ivan Yarych


I just came up with this little helper function:

function get(&$var, $default=null) {     return isset($var) ? $var : $default; } 

Not only does this work for dictionaries, but for all kind of variables:

$test = array('foo'=>'bar'); get($test['foo'],'nope'); // bar get($test['baz'],'nope'); // nope get($test['spam']['eggs'],'nope'); // nope get($undefined,'nope'); // nope 

Passing a previously undefined variable per reference doesn't cause a NOTICE error. Instead, passing $var by reference will define it and set it to null. The default value will also be returned if the passed variable is null. Also note the implicitly generated array in the spam/eggs example:

json_encode($test); // {"foo":"bar","baz":null,"spam":{"eggs":null}} $undefined===null; // true (got defined by passing it to get) isset($undefined) // false get($undefined,'nope'); // nope 

Note that even though $var is passed by reference, the result of get($var) will be a copy of $var, not a reference. I hope this helps!

like image 21
stepmuel Avatar answered Oct 05 '22 20:10

stepmuel