跪求php 多维数组操作的问题,哪位好心人帮帮忙

number123456 2010-07-14 12:11:10
数组
$a = array(
"a"=>array(
"ab"=>array(
"ab1" => "d",
"ab2" => "dd",
),
"ac"=>array(
"ac1" => "d",
"ac2" => "dd",
)
),
"b" => array(
"bb"=>array(
"bb1" => "d",
"bb2" => "dd",
),
"bc"=>array(
"bc1" => "d",
"bc2" => "dd",
)
)

);

我想根据数组
$d = array("a","ac","ac1");
来改变
$a["a"]["ac"]["ac1"] 的值,原来是"d",我要改成"c"
请问要怎么来实现啊。
...全文
123 点赞 收藏 17
写回复
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
kyzy_yy_pm 2010-07-14
$d = array("a","ac","ac1");
来改变
$a["a"]["ac"]["ac1"] 的值,原来是"d",我要改成"c"

$a[$d[0]][$d[1]][$d[2]] = 'c';
回复
number123456 2010-07-14
对不起,楼上的,我说过,$d变量是不因定的,也就是说,有多少个key是未知的,只能知道他的格式是一维数组。
回复
xingworld 2010-07-14

if ( isset( $a[ $d[0] ][ $d[1] ][ $d[2] ] ) )
{
$a[ $d[0] ][ $d[1] ][ $d[2] ] = 'c';
}
回复
number123456 2010-07-14
楼不起,我没有说清楚
$d = array("a","ac","ac1");
这是个不定数量的组组,有可能是
$d = array("a");

$d = array("a","ac");
而且还有
$d = array("a","ad");这种可能,
就是说在$a["a"]中没有ad这一索引,那就默认把$a["a"]改掉


回复
amani11 2010-07-14


$d = array("a","ac","ac1");
if($a[$d[0]][$d[1]][$d[2]] == 'd')
{
$a[$d[0]][$d[1]][$d[2]] = 'c';
}

回复
CunningBoy 2010-07-14
$a[$d[0]][$d[1]][$d[2]] = "c";
回复
number123456 2010-07-14
我换别的方法去做了,谢谢大家
回复
zhang6464 2010-07-14
感觉不该出错啊。。
[Quote=引用 10 楼 number123456 的回复:]
对不起,我刚测试了,出现错误
Notice: Use of undefined constant a - assumed 'a' in E:\wamp\www\2.0\index.php(38) : eval()'d code on line 1
[/Quote]
回复
number123456 2010-07-14
请问用eval会不会出现啥漏洞啊?有什么方法可以防一下吗?
回复
amani11 2010-07-14
[Quote=引用 10 楼 number123456 的回复:]

对不起,我刚测试了,出现错误
Notice: Use of undefined constant a - assumed 'a' in E:\wamp\www\2.0\index.php(38) : eval()'d code on line 1
[/Quote]

改成eval('$a["' . join('"]["', $d) . '"] = $c;');
回复
max_soft 2010-07-14
我错了,再想想。。。
回复
max_soft 2010-07-14

$before_k = '';
$changed = 0;
foreach($d as $k) {
global $before_k;
if(!empty($a[$k]) && is_array($a[$k])) {
continue;
} else {
$a[$before_k] = 'c';//不知道你这个C是怎么来的
$changed = 1;
break;
}
$before_k = $k
}
if(!$changed && $a[$before_k]) {
$a[$curent_k] = 'c';
}


应该有个break
回复
max_soft 2010-07-14

$before_k = '';
$changed = 0;
foreach($d as $k) {
global $before_k;
if(!empty($a[$k]) && is_array($a[$k])) {
continue;
} else {
$a[$before_k] = 'c';//不知道你这个C是怎么来的
$changed = 1;
}
$before_k = $k
}
if(!$changed && $a[$before_k]) {
$a[$curent_k] = 'c';
}
回复
number123456 2010-07-14
对不起,我刚测试了,出现错误
Notice: Use of undefined constant a - assumed 'a' in E:\wamp\www\2.0\index.php(38) : eval()'d code on line 1
回复
number123456 2010-07-14
谢谢楼上的,你的意思是我想要的,
请问,用eval这样的操作会不会存在安全隐患?
回复
xuzuning 2010-07-14
$a = array(
"a"=>array(
"ab"=>array(
"ab1" => "d",
"ab2" => "dd",
),
"ac"=>array(
"ac1" => "d",
"ac2" => "dd",
)
),
"b" => array(
"bb"=>array(
"bb1" => "d",
"bb2" => "dd",
),
"bc"=>array(
"bc1" => "d",
"bc2" => "dd",
)
)

);

$d = array("a","ac","ac1");
$c = 'abcd';
eval('$a[' . join('][', $d) . '] = $c;');

print_r($a);


Array
(
[a] => Array
(
[ab] => Array
(
[ab1] => d
[ab2] => dd
)

[ac] => Array
(
[ac1] => abcd //这里被改变了
[ac2] => dd
)

)

[b] => Array
(
[bb] => Array
(
[bb1] => d
[bb2] => dd
)

[bc] => Array
(
[bc1] => d
[bc2] => dd
)

)

)

回复
number123456 2010-07-14
再重复一次,$d是不固定的,如果就只是现在这么固定的值的话很容易了,但是我要求的是一个方法
function setArray( $a,$d )
{
用什么方法能通过$d的数组来改变$a相应的值
}
回复
发动态
发帖子
基础编程
创建于2007-09-28

2.1w+

社区成员

从PHP安装配置,PHP入门,PHP基础到PHP应用
申请成为版主
社区公告
暂无公告