doulu3865 2017-03-30 08:19
浏览 564
已采纳

PHP键值对数组到字符串

Let's say I have an array that looks like this:

$args = array(
  'format' => 'file',
  'type' => 'page',
  'name' => 'Projects',
  'template' => 'default'
);

To get the values from the array keys I need to do this:

echo $args['format'];
echo $args['type'];
echo $args['name'];
echo $args['template'];

While it works, it feels more messy than needed. Is there a proper way to somehow convert it to this?

echo $format;
echo $type;
echo $name;
echo $template;

I'm aware of list but that only uses values and not keys.

  • 写回答

2条回答 默认 最新

  • douqiandai4327 2017-03-30 08:20
    关注

    Are you looking for http://php.net/manual/en/function.extract.php ?

    $args = array(
      'format' => 'file',
      'type' => 'page',
      'name' => 'Projects',
      'template' => 'default'
    );
    extract($args);
    
    echo "$format, $type, $name, $template";
    

    Output:

    file, page, Projects, default

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn