创建一个从PHP中数组键的新变量 [英] Create new variables from array keys in PHP

查看:132
本文介绍了创建一个从PHP中数组键的新变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个数组,像这样:

  $富=阵列('第一'=>'1',
             第二个=> '第二',
             '第三'=> '3');

我怎样才能挑选出钥匙的数组,使他们自己的变量?
例如,阵列 $ foo的将变成:

  $第一='1';
$秒='第二';
$第三='3';

我问这个,因为我创造一个MVC框架,来帮助我的OOP,我想用户变量传递给视图加载功能,这将允许用户使用模板中的变量,而不必知道什么阵列被调用。

例如:

  $阵列=阵列('标题'=>'!我的博客[...]);
$这个 - >负载>查看('view.php',$阵列);

view.php:

 回声$称号;

输出:


  

我的博客!



解决方案

 < PHP提取物($数组); ?>

http://php.net/extract

Suppose I have an array, like this:

$foo = array('first' =>  '1st',
             'second' => '2nd',
             'third' =>  '3rd');

How can I pick the keys out of the array and make them their own variables? For example, the array $foo would become:

$first = '1st';
$second = '2nd';
$third = '3rd';

I ask this because I am creating an MVC framework to help with my OOP, and I would like the user to pass a variable to the View loading function, which will allow the user to use variables in the template without having to know what the array was called.

For example:

$array = array('title' =>  'My blog!' [...]);
$this->load->view('view.php', $array);

view.php:

echo $title;

Output:

My blog!

解决方案

<?php extract($array); ?>

http://php.net/extract

这篇关于创建一个从PHP中数组键的新变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆