views:

231

answers:

5

I don't know how about you, but I'm not very fond of the way arrays are constructed in PHP. I have this feeling that I use array keyword way too often and that array($k => $v) or e.g. array($k1=>array($k2=>$v)) are way too long given usefulness of maps. (Moreover, recently I've learned JS way of doing it and now I really am jealous)

The best I could come up with to remedy this 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 permit using => operator.

Any other ideas?

+1  A: 

Although yes, it's nice having the array definition syntax in Javascript, I personally am not too bothered by having to type the extra 4 characters which you save in this or any other shortcutting method.

nickf
+3  A: 

There is no shorthand syntax for declaring arrays in PHP. It's a feature I would like to see, but I very much doubt it will happen.

It's been discussed a lot by the PHP developers and the PHP community, but it was never implemented. A good starting point if you want to see how the discussion unfolded is available on the PHP wiki: http://wiki.php.net/rfc/shortsyntaxforarrays

For now, you will have to put up with typing a handful of extra characters.

Alex Barrett
A: 

dude, stop that. There's no clarity/simplicity you can get by doing this. The rest members of your project will WTF all the way.

[I'm sorry. I want to post this as comment by my points is way too low]

nil
A: 

PHP is the most readable language I know. I guess that why the array syntax is like that

andho
A: 

Use Texter or any decent editor with templates/macros. E.g.:

[]+Tab ---> array({cursor})

If you're truly obsessed, make a json_decode macro to run a selection through this:

<?php var_export(json_decode(stream_get_contents(STDIN), true));

Just don't put JSON in your PHP code because you'd rather look at JSON...

mrclay