Print the keys of an array

前端 未结 8 705
旧巷少年郎
旧巷少年郎 2020-11-27 21:07

I could not figure out how to pass a variable number of variables into a function. I thought passing in an array and using the array keys for the variables names could repla

8条回答
  •  被撕碎了的回忆
    2020-11-27 21:44

    Passing an associative array to a function is a reasonable way to pass in a variable number of parameters.

    foreach ($parameters as $key => $value) {
      echo $key . ' = ' . $value . '
    '; }

    Alternatively you could pass in an instance of stdClass (casting the argument to an object). But an array does the job.

    I assume your array keys aren't constants, in which case they should be quoted strings:

    $parameters['day'] = 1;
    $parameters['month'] = 8;
    $parameters['year'] = 2010; 
    

提交回复
热议问题