functions to get/set values in multidimensional arrays dynamically(动态获取/设置多维数组中的值的函数)
问题描述
我正在尝试用 php 编写购物车,但在多维数组中获取/设置值时遇到问题.我将当前订单保存在 $_SESSION['basket']
中.看起来像这样:
I am trying to write a shoping cart in php and I have a problem with get/set values in multidimentional arrays.
I keep the current order in $_SESSION['basket']
. It looks like that:
[basket] => Array
(
[0] => Array
(
[pid] => 3
[name] => Camera
[price] => 200.99
[quantity] => 1
)
[1] => Array
(
[pid] => 5
[name] => Computer
[price] => 320.99
[quantity] => 1
[extras] => Array
(
[0] => Array
(
[pid] => 86
[name] => RAM
[price] => 99
[qty] => 1
)
[1] => Array
(
[pid] => 98
[name] => CD-ROM
[price] => 19.99
[qty] => 1
)
)
)
)
每一项都存储为一个子数组.我有一个函数,它检查给定的项目是否存在于 basket
数组中并返回它的路径.例如,如果我想检查具有 id
98
(CD-Rom) 的产品,该函数返回以下路径:1:extras:1代码>.
Every item is stored as a subarray. I have a function, which checks if a given item exists in the basket
array and returns the path to it. For example, if I want to check for a product with id
98
(CD-Rom), the function returns the following path: 1:extras:1
.
如果我想在数组中获取或设置一个值,我无法弄清楚如何使用路径.是否可以在不使用 eval()
的情况下构造数组键的路径?我有这些功能:
I cant figure out how to use the path if I want to get or a set a value in the array. Is it possible to construct the path to an array key, without the use of eval()
? I have these functions:
function get_val($array, $path, $key) {
//some code
return eval('return '.$array.$path.$key.';');
}
所以,$price = get_val($_SESSION['basket'], $path, 'price')
;应该返回 CD-ROM 的价格 (19.99)
So, $price = get_val($_SESSION['basket'], $path, 'price')
; should return the price for CD-ROM (19.99)
function set_val($array, $path, $key, $value) {
//some code
$str = eval(''.$array.$path.$key.';');
$str = $value;
}
set_val($_SESSION['basket'], $path, 'price', '30');
将 CD-ROM 的价格设置为 30.
set_val($_SESSION['basket'], $path, 'price', '30');
will set the price for CD-ROM to 30.
有没有更好的方法来做到这一点?
Is there a better way for doing this?
谢谢.
推荐答案
这是我前段时间微调的代码:
Here you go a code I have finetuned some time ago:
function get_val($array,$path) {
for($i=$array; $key=array_shift($path); $i=$i[$key]) {
if(!isset($i[$key])) return null;
}
return $i;
}
function set_val(&$array,$path,$val) {
for($i=&$array; $key=array_shift($path); $i=&$i[$key]) {
if(!isset($i[$key])) $i[$key] = array();
}
$i = $val;
}
看到这个测试示例,我相信这就是你要找的:
See this test example, I believe it is what you are looking for:
$data = array("x"=>array("y"=>array("z"=>"foo")));
echo get_val($data,array("x","y","z")); // foo
set_val($data,array("x","y","u"),"bar"); // $data["x"]["y"]["u"] = "bar";
这篇关于动态获取/设置多维数组中的值的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:动态获取/设置多维数组中的值的函数
基础教程推荐
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01