gpt4 book ai didi

php - 从 PHP 中的数组键创建新变量

转载 作者:IT王子 更新时间:2023-10-29 00:49:48 26 4
gpt4 key购买 nike

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

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

如何从数组中选择键并将它们设为自己的变量?例如,数组 $foo 会变成:

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

我问这个是因为我正在创建一个 MVC 框架来帮助我的 OOP,我希望用户将一个变量传递给 View 加载函数,这将允许用户在模板中使用变量而不必知道数组叫什么。

例如:

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

view.php:

echo $title;

输出:

My blog!

最佳答案

关于php - 从 PHP 中的数组键创建新变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4916510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com