Print the keys of an array

前端 未结 8 647
旧巷少年郎
旧巷少年郎 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:40
    foreach(array_keys($parameters) as $key) {
       echo $key.'<br/>';
    }
    
    0 讨论(0)
  • 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 . '<br>';
    }
    

    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; 
    
    0 讨论(0)
提交回复
热议问题