PHP: less ugly syntax for named parameters / arrays?

前端 未结 4 1352
暖寄归人
暖寄归人 2021-01-14 13:48

Here\'s what I am trying to accomplish:

function foo($args) {
 switch($args[\'type\']) {
  case \'bar\':
  bar($args[\'data\']);   // do something
  break;
          


        
4条回答
  •  隐瞒了意图╮
    2021-01-14 14:48

    You could create a JSON-encoded string and use json_decode() to convert it into a variable. This has syntax very similar to the Python-like syntax you mentioned.

    $argstr = '{"type" : "bar", "data" : [1, 2, 3], "data2" : [5, 10, 20]}';
    $buildArgs = json_decode($argstr, true);
    

    EDIT: Updated code to accommodate @therefromhere's suggestion.

提交回复
热议问题