Hello,
I am using arrays in PHP to modify xml data and write it back. This is the xml structure (simplified for demonstration purpose):
<docs>
<folder>
<name>Folder name</name>
<date>20.06.2009</date>
<folder>
<name>Subfolder1</name>
<date></date>
</folder>
<folder>
<name>Subfolder1</name>
<date></date>
</folder>
<file>
<name></name>
</file>
</folder>
<name></name>
<date></date>
</docs>
Using this code, this is then parsed and transformed into a multidimensional array:
Array
(
[docs] => Array
(
[_c] => Array
(
[folder] => Array
(
[_c] => Array
(
[name] => Array
(
[_v] => Folder name
)
[date] => Array
(
[_v] => 20.06.2009
)
[folder] => Array
(
[0] => Array
(
[_c] => Array
(
[name] => Array
(
[_v] => Subfolder1
)
[date] => Array
(
[_v] =>
)
)
)
[1] => Array
(
[_c] => Array
(
[name] => Array
(
[_v] => Subfolder1
)
[date] => Array
(
[_v] =>
)
)
)
)
[file] => Array
(
[_c] => Array
(
[name] => Array
(
[_v] =>
)
)
)
)
)
[name] => Array
(
[_v] =>
)
[date] => Array
(
[_v] =>
)
)
)
)
Lengthy, I know. But now to the actual issue. If I want to add another file to a sub folder called Subfolder2 in this case, it's not a problem to do it by hand when u see the structure:
array_push($array['docs']['_c']['folder']['_c']['folder'][1], $newfile);
Now when I want to do it via the function that only knows a path to the folder (like docs/Folder name/Subfolder2), the algorithm has to analyze the array structure (check the name of each [folder], check if there is one or more folders ['_c'] or [number]) - all good, but I cannot find a way to create a variable that would have an "array" path for that new file.
I was thinking somewhere along these lines:
$writepath = "['docs']['_c']['folder']...[1]"; // path string
array_push($array{$writepath}, $newfile);
Of course this is not a valid syntax.
So, how can I make a variable that contains a path through the array elements? I did a bit of research on w3c and php.net finding no helpful info on multidimensional arrays...
If anyone has any other suggestions regarding structure, xml transformation/manipulation etc. by all means, I know it is far from sufficient way of data handling.
Thanks for any input,
Erik
Edit: Regarding the reference, is it possible to reference the reference? As that would be the way to move the 'pointer' through a set of arrays? Something as such:
$pointer = &$array['docs'];
if (key($pointer) == '_c') { $pointer = &$pointer['_c']; }
else (
// create an array with '_c' key instead of empty '_v' array
)
This syntax does not work.
Edit: The syntax works, never mind... Thanks for all your help guys!