Reputation: 3163
Here's an example of my array:
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) ""
}
}
}
}
}
As an example, I want to change the value of art
where the id
is id-1
and where the pos
is 163
.
I started, but got stuck somewhere deep...
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?!?!
}
}
}
}
}
}
}
Am I on the right track? What's next? Anyone that is able to point me in the right direction?
Upvotes: 0
Views: 519
Reputation: 861
The way you define this array makes it hard to see what is going on. You could write it like this:
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'=> ""
),
),
),
),
);
This way you can for example see, that the 'data' array introduces a superfluent nesting level. To answer your question: You can access elements in multidimensional arrays like this:
foreach ($arr as $key=>value)
if ($value['id'] == 'id-1')
foreach($value as $key2=>$value2)
if ($value2['pos'] == 163)
$value2['art'] = $newArtValue;
I agree with the comments, that you should change your structure. For this loop, using the 'id' and 'pos' values as KEYS would make the iterations unnecessary:
So if you write your array like this (loosing no information):
$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'=> ""
),
),
),
);
Changing the value as you asked is as simple as:
$arr['id-1'][163]['art'] = $newArtValue;
Upvotes: 1
Reputation: 667
Use array_recursive() in PHP
<?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";
}
}
?>
Upvotes: 1
Reputation: 1580
You should really use objects when you start dealing with so deeply nested arrays. If you have no other choice though, you can create a recursive function that returns the subarray of n-th level with some parameter that lets you control how deep you want to go.
Something like this perhaps?
Upvotes: 1