从 PHP 中的数组键创建新变量 [英] Create new variables from array keys in PHP
本文介绍了从 PHP 中的数组键创建新变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个数组,如下所示:
Suppose I have an array, like this:
$foo = array('first' => '1st',
'second' => '2nd',
'third' => '3rd');
如何从数组中挑选键并使它们成为自己的变量?例如,数组 $foo
将变成:
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';
我问这个是因为我正在创建一个 MVC 框架来帮助我的 OOP,我希望用户将一个变量传递给视图加载函数,这将允许用户在模板中使用变量而无需知道这个数组叫什么.
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.
例如:
$array = array('title' => 'My blog!' [...]);
$this->load->view('view.php', $array);
view.php:
echo $title;
输出:
我的博客!
推荐答案
<?php extract($array); ?>
http://php.net/manual/en/function.extract.php
这篇关于从 PHP 中的数组键创建新变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文