Abbreviation for PHP Array ()
I don't know about you, but I don't really like how arrays are built in PHP. I have a feeling that I use the keyword too often array
and that array($k => $v)
or, for example, are array($k1=>array($k2=>$v))
too long given the usefulness of the maps. (Moreover, I recently learned a JS way to do this and now I'm really jealous)
The best I could fix is:
function a() { // array
return func_get_args();
}
and
function h() { // hash
$array=array();
for($i=0; $i<func_num_args()-1; $i+=2) {
$array[func_get_arg($i)]=func_get_arg($i+1);
}
return $array;
}
... but they don't allow the use of an operator =>
.
Any other ideas?
source to share
Since PHP 5.4 , the shorthand syntax for arrays is supported with [
and ]
. Your examples:
array($k => $v)
array($k1=>array($k2=>$v))
can now be written as:
[$k => $v] [$k1 => [$k2 => $v]]
There is no shorthand syntax for declaring arrays in PHP. This is a feature I would like to see, but I highly doubt it will happen.
This has been discussed by many PHP developers and the PHP community, but it has never been implemented. A good starting point if you want to see how a detailed discussion is available on the PHP wiki: http://wiki.php.net/rfc/shortsyntaxforarrays
Now you have to come to terms with a set of a few extra characters.
source to share
Use Texter or any suitable template / macro editor. For example:.
[]+Tab ---> array({cursor})
If you are really obsessed, make a json_decode macro to trigger the selection with this:
<?php var_export(json_decode(stream_get_contents(STDIN), true));
Just don't put JSON in your PHP code because you better take a look at JSON ...
source to share