php中怎样循环遍历数组元素,PHP循环遍历数组
Array
(
[0] => Array
(
[0] => A
[1] => B
[2] => C
)
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[2] => Array
(
[0] => apple
[1] => orange
[2]
我有一个如下所示的数组($array): Array ( [0] => Array ( [0] => A [1] => B [2] => C ) [1] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [2] => Array ( [0] => apple [1] => orange [2] => banana ) ) 使用CreateXML我想获取数组并创建一个XML文档,但我遇到了一些问题.我正在使用foreach从每个数组中获取第一个值,因为我希望[0] A [0] 1 [0] apple位于一个元素中,依此类推. 使用我现在的代码它适用于一个元素php循环,我该怎么做才能生成所有元素?迷失在循环中,只是无法正确…感谢您的帮助! public function CreateXML($array){ foreach ($array as $arr) { $array2[] = $arr[0]; } $xmlDoc = new DOMDocument(); $root = $xmlDoc->appendChild( $xmlDoc->createElement("rootelement")); $tag = $root->appendChild( $xmlDoc->createElement("element")); $tag->appendChild( $xmlDoc->createElement("Letter", $array2[0])); $tag->appendChild( $xmlDoc->createElement("Number", $array2[1])); $tag->appendChild( $xmlDoc->createElement("Fruit", $array2[2])); header("Content-Type: text/plain"); $xmlDoc->formatOutput = true; echo $xmlDoc->saveXML(); } (编辑:上海站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |