函数动态地获取/设置多维数组中的值 [英] functions to get/set values in multidimensional arrays dynamically
问题描述
我试图在php中编写购物车,并且在多维数组中存在get / set值的问题。
我将当前订单保存在 $ _ SESSION ['basket']
中。它看起来像这样:
[basket] =>数组
(
[0] =>数组
(
[pid] => 3
[名称] =>相机
[价格] => 200.99
[数量] => 1
)
[1] =>数组
(
[ pid] => 5
[名称] =>计算机
[价格] => 320.99
[数量] => 1
[ =>阵列
(
[0] =>阵列
(
[pid] => 86
[名称] => RAM
[price] => 99
[qty] => 1
)
[1] =& GT; Array
(
[pid] => 98
[name] => CD-ROM
[price] => 19.99
[qty] => ; 1
)
)
)
)
每个项目都存储为子阵列。我有一个函数,它检查篮子
数组中是否存在给定项目并返回它的路径。例如,如果我想检查产品 id
98
(CD-Rom),该函数返回以下路径: 1:extras:1
。
我不知道如何使用路径,如果我想获得或在数组中设置一个值。是否有可能构建数组键的路径,而不使用 eval()
?我有这些函数:
pre $函数get_val($ array,$ path,$ key){
//一些代码
return eval('return'。$ array。$ path。$ key。';');
$ / code>
所以, $ price = get_val($ _ SESSION [ 'basket'],$ path,'price')
;应该返回CD-ROM的价格(19.99)
pre $函数set_val($ array,$ path,$ key,$ value) {
//一些代码
$ str = eval(''。$ array。$ path。$ key。';');
$ str = $ value;
$ / code>
set_val($ _ SESSION ['basket'], $ path,'price','30');
将CD-ROM的价格设置为30美元。 有没有更好的方法为了做到这一点?
谢谢。 我前段时间已经调整过的代码:
pre $ 函数get_val($ array,$ path){
for($如果(!isset($ i [$ key]))返回null;如果($ is $($ key)
}
返回$ i; $
$ b $($&$; $ array $; ; $ i =& $ i [$ key]){
if(!isset($ i [$ key]))$ i [$ key] = array();
}
$ i = $ val;
}
请参阅这个测试示例,我相信这是您正在寻找的内容:
$ 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;
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
)
)
)
)
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
.
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.';');
}
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');
will set the price for CD-ROM to 30.
Is there a better way for doing this?
Thank you.
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";
这篇关于函数动态地获取/设置多维数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!