这是我的数组的一个例子:
array(4) {
[0]=>
array(2) {
["id"]=>
string(4) "id-1"
["articles"]=>
array(2) {
[0]=>
array(1) {
["data"]=>
array(6) {
["art"]=>
string(6) "108108"
["type"]=>
string(0) ""
["pos"]=>
string(3) "125"
["kleur"]=>
string(4) "3750"
["height"]=>
string(3) "179"
["foto"]=>
string(2) "01"
}
}
[1]=>
array(1) {
["data"]=>
array(6) {
["art"]=>
string(0) ""
["type"]=>
string(0) ""
["pos"]=>
string(3) "163"
["kleur"]=>
string(0) ""
["height"]=>
string(0) ""
["foto"]=>
string(0) ""
}
}
}
}
[2]=>
array(2) {
["id"]=>
string(7) "ui-id-2"
["articles"]=>
array(2) {
[0]=>
array(1) {
["data"]=>
array(6) {
["art"]=>
string(0) ""
["type"]=>
string(0) ""
["pos"]=>
string(3) "944"
["kleur"]=>
string(0) ""
["height"]=>
string(0) ""
["foto"]=>
string(0) ""
}
}
[1]=>
array(1) {
["data"]=>
array(6) {
["art"]=>
string(0) ""
["type"]=>
string(0) ""
["pos"]=>
string(3) "586"
["kleur"]=>
string(0) ""
["height"]=>
string(0) ""
["foto"]=>
string(0) ""
}
}
}
}
}
例如,我想更改art
为id
id-1
pos
为163
的{{1}}的值。
我开始了,但被困在某个地方......
foreach($data as $key => $value) {
if($value['id'] === "id-1") {
foreach($value as $tweek => $tweev) {
foreach($tweev as $driek => $driev) {
foreach($driev as $vierk => $vierv) {
if($vierv['pos'] === "163") {
// This is the right article! I think...
echo $vierv['pos'] . "<br />"; // shows 163!
foreach($vierv as $vijfk => $vijfv) {
// What to do?!?!
}
}
}
}
}
}
}
我是否在正确的轨道上?下一步是什么?能指出正确方向的人吗?
答案 0 :(得分:1)
在PHP中使用array_recursive()
<?php
$a1=array("a"=>"red","pos"=>"364");
$a2=array($a1,"1"=>"blue","2"=>"yellow");//one array inside another to simulate your multi depth array
array_walk_recursive($a2,"myfunction");//use array_recursive
function myfunction($value,$key)
{
if ($key == 'pos' && $value == '364') { //set your condition here
echo "change your key or value here";
}
}
?>
答案 1 :(得分:1)
定义此数组的方式使得很难看到发生了什么。你可以这样写:
arr = array(
0=>array(
'id'=>"id-1",
'articles'=> array(
0=>array(
'data'=> array(
'art'=> "108108"
'type'=> ""
'pos'=> "125"
'kleur'=> "3750"
'height'=> "179"
'foto'=> "01"
),
),
1=> array(
'data'=> array(
'art'=> ""
'type'=> ""
'pos'=> "163"
'kleur'=> ""
'height'=> ""
'foto'=> ""
),
),
),
),
2=> array(
'id'=>"ui-id-2"
'articles'=> array(
0=> array(
'data'=> array(
'art'=> ""
'type'=> ""
'pos'=> "944"
'kleur'=> ""
'height'=> ""
'foto'=> ""
),
),
1=> array(
'data'=> array(
'art'=> ""
'type'=> ""
'pos'=> "586"
'kleur'=> ""
'height'=> ""
'foto'=> ""
),
),
),
),
);
这样你可以看到'data'数组引入了一个superfluent嵌套级别。回答你的问题:您可以访问多维数组中的元素,如下所示:
foreach ($arr as $key=>value)
if ($value['id'] == 'id-1')
foreach($value as $key2=>$value2)
if ($value2['pos'] == 163)
$value2['art'] = $newArtValue;
我同意这些意见,你应该改变你的结构。对于这个循环,使用'id'和'pos'值作为KEYS将使迭代不必要:
所以如果你这样编写你的数组(没有信息):
$arr = array(
'id-1'=>array(
'articles'=> array(
125=>array(
'art'=> "108108"
'type'=> ""
'kleur'=> "3750"
'height'=> "179"
'foto'=> "01"
),
163=> array(
'art'=> ""
'type'=> ""
'kleur'=> ""
'height'=> ""
'foto'=> ""
),
),
),
'ui-id-2'=> array(
'articles'=> array(
944=> array(
'art'=> ""
'type'=> ""
'kleur'=> ""
'height'=> ""
'foto'=> ""
),
586=> array(
'art'=> ""
'type'=> ""
'kleur'=> ""
'height'=> ""
'foto'=> ""
),
),
),
);
按要求更改值非常简单:
$arr['id-1'][163]['art'] = $newArtValue;
答案 2 :(得分:0)
当您开始处理如此深层嵌套的数组时,您应该真正使用对象。如果您没有其他选择,则可以创建一个递归函数,该函数返回第n级子数组,其中包含一些参数,可以控制您想要的深度。
或许 this ?