dryk50495 2019-01-10 09:43
浏览 85
已采纳

将变量名和内容保存到php中的关联数组中

Can i define an associative array using name and value of some variables at the same time and without looping in a simple elegant way?

I know it is possible taking variable names or setting it manually is not hard at all, but i think there might be a function to do this all at once. Or there should be one, but i didnt found any

Im looking a function similar to:

$ar_var= to_associative_array($name, $id, $start_date, $end_date, $status, $details);

... and being able to print something like:

echo $ar_var['status'];
echo $ar_var[5];//Usually you would use this to access this data

... or a vardump showing something like

name => 'John Doe'
id => 'FX1678798Z'
start_date = > '27/03/2018'
status => 'delivered'
  • 写回答

1条回答 默认 最新

  • dougou2937 2019-01-10 09:52
    关注

    You can use compact() to accomplish this.

    Assuming the variables you are using are declared and have assigned values, you can use it like this:

    $ar_var = compact('name', 'id', 'start_date', 'end_date', 'status', 'details');
    

    Example: https://3v4l.org/8jnCe

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记