下面是我的数组示例:
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
。
我开始了,但被困在了一个很深的地方。。。
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?!?!
}
}
}
}
}
}
}
我走对了吗?下一步是什么?有谁能为我指明正确的方向吗?
当您开始处理嵌套如此之深的数组时,您应该真正使用对象。不过,如果你别无选择,你可以创建一个递归函数,返回第n级的子数组,并使用一些参数来控制你想要深入的程度。
可能是类似这个的东西?
在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";
}
}
?>
定义这个数组的方式很难看出发生了什么。你可以这样写:
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'=> ""
),
),
),
),
);
例如,通过这种方式,您可以看到"数据"数组引入了一个超流嵌套级别。回答您的问题:您可以访问多维数组中的元素,如下所示:
foreach ($arr as $key=>value)
if ($value['id'] == 'id-1')
foreach($value as $key2=>$value2)
if ($value2['pos'] == 163)
$value2['art'] = $newArtValue;
我同意你的意见,你应该改变你的结构。对于这个循环,使用"id"one_answers"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;