Some kind of followup to my last question: for loop - move deeper on numeric key in multidimensional array
I have this array as input:
Array
(
[0] => apl_struct Object
(
[funcname] => say
[args] => Array
(
[0] => Array
(
[0] => apl_struct Object
(
[funcname] => text
[args] => Array
(
[value] => hello
)
)
)
)
)
)
I now have 2 functions working for me. One is a func only for getting the next key/value in an associative array. None of the next(), prev(), etc. were working for me like on indexed arrays:
function getnext($array, $key) {
$keys = array_keys($array);
if ((false !== ($p = array_search($key, $keys))) && ($p < count($keys) - 1)) {
return array('key' => $keys[++$p], 'value' => $array[$keys[$p]]);
} else {return false;}
}
The next function is my executer or constructer. he creates a semi-xmlstruct for me. I tried to add recursion for skipping the numeric key. They're obviously nonsense and can be skipped.
I then want to check if all the values of the non-numeric keys are arrays or not. If it is an array it indicates arguments to be followed and output should look like: INPUT.
If not, it's either the functionname (funcname) or indeed a real value for us like "hello".
function arr2xml($array, $level = 1, $pos = 1) {
$xml = '';
foreach ($array as $key => $value) {
if (is_object($value)) {$value = get_object_vars($value);}// convert object to array
if (is_numeric($key)) {
$xml .= arr2xml($value);
} else {
if (!is_array($value)) {
switch ($key) {
case 'funcname':
$nextkey = getnext($array, $key);
$xml .= str_repeat("\t", $level) . "<apl:$value>
";
$xml .= arr2xml($nextkey['value'], $level++);
$xml .= str_repeat("\t", $level) . "</apl:$value>
";
break;
case 'value':
$xml .= str_repeat("\t", $level) . "\t$value
";
break;
}
} else {
$xml .= str_repeat("\t", $level) . "<$key pos='$pos'>
\t";
$xml .= arr2xml($value, $level++, $pos++);
$xml .= str_repeat("\t", $level) . "</$key>
";
}
}
}
return $xml;
}
but what I am getting out of this so far is this: the function name was inserted right. it is say and text. also, in some wild circumstances, the -tag and the value are executed properly.
<apl:say>
<apl:text>
hello
</apl:text>
<args pos='1'>
hello
</args>
</apl:say>
<args pos='1'>
<apl:text>
hello
</apl:text>
<args pos='1'>
hello
</args>
</args>
</xml>
for me it looks like the recursion isn't really working. Am i missing something here? I've tried to rebuild it from previous mentioned post.
Also I'm wondering about the multiple output I am getting here. The tags seem to get filled right, but the actual arrangement is quite confusing for me.
I was expecting the output to look like this:
<apl:say>
<args pos='1'>
<apl:text>
<args pos='1'>
hello
</args>
</apl:text>
</args>
</apl:say>
Thanks in advance