• 主页

# 跪求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

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");

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

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]

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';
}
``````

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

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

function setArray( \$a,\$d )
{

}

2.1w+