Hello guys,
Just a quick quesiton
I want to only update some arrays within the existing array,
$oldArray = array('ArrTitle1' =>array('arrayValue1','arryValue2') 'ArrTitle2'=>array('arrayValue1','arrayValue2) .....'ArrTitle5'=>array('arrayValue1','arrayValue2')......'ArrTitle18'=>array('arrayValue1','arrayValue2'))
//sizeOf($oldArray) is 19
$curArray =array('ArrTitle5'=>array('NEWarrayValue1','NEWarrayValue2') 'ArrTitle6'=>array('NEWarrayValue1','NEWarrayValue2') .......'arrTitle15'=>array('NEWarrayValue1','NEWarrayValue2')
//sizeOf($curArray) is 15
I was thinking of using combination of foreach() and if statement to find the key and replace with the value , such as following
foreach($oldArray as $key=>$val){
if ($key == 'arrTitle5' || $key == 'arrTitle6' ){ //line 2
foreach ($val as $valueKey=>$value){
$oldArray[$valueKey] = $value;
}
}
}
As you can see from the above code, on line 2
Its not very smart as if I want to update 10 arrays then it will be a long list and I see it as a quick fix, which cannot be reused in the future.
Just wondering if anyone know how to make it more dynamic and less static.
Greatly appreciate it
How to update multidimensional arrays
Moderator: General Moderators
How to update multidimensional arrays
Last edited by unidude on Fri Jul 29, 2005 1:02 am, edited 3 times in total.
- harrisonad
- Forum Contributor
- Posts: 288
- Joined: Fri Oct 15, 2004 4:58 am
- Location: Philippines
- Contact:
first of all, use php tags for displaying your code.
Diving deep to the deepest array values can be a tiring job but easy to do.
Diving deep to the deepest array values can be a tiring job but easy to do.
Code: Select all
// searching and retrieving
foreach($aray as $parent){
foreach($parent as $child){
foreach($child as $grandchild){
// etc...
}
}
}
// retrieving
$value = $aray['parent']['child']['grandchild'];
// updating
$aray['parent']['child']['grandchild'] = $value;Thans harrisonad
For your fast reply.
I used your code doesnt seem to work for my purpose but you have gave me ideas to improve my code,this is the following
This will replace the existing one and wont erase the one that has not been found.
Regards,
For your fast reply.
I used your code doesnt seem to work for my purpose but you have gave me ideas to improve my code,this is the following
Code: Select all
foreach ($curArray as $parentKey=>$parentVal){
foreach ($parentVal as $childKey=>$childVal){
$oldArray[$parentKey][$childKey] = $curArray[$parentKey][$childKey];
}
}Regards,
- harrisonad
- Forum Contributor
- Posts: 288
- Joined: Fri Oct 15, 2004 4:58 am
- Location: Philippines
- Contact:
Code: Select all
foreach ($curArray as $parentKey=>$parentVal){
foreach ($parentVal as $childKey=>$childVal){
$oldArray[$parentKey][$childKey] = $childVal; // <-- simpler
}
}