How to update multidimensional arrays
Posted: Thu Jul 28, 2005 8:13 pm
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
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