【发布时间】:2016-12-05 17:39:18
【问题描述】:
array_push($typedict[$current], "value");
这里似乎没有做任何事情,我输出了数组的关联数组($typedict),但它们都是空的(array())。我用 echo 打印出当前的关联索引,以确认它是正确的(总是如此)。
由于最后的打印 chelc 状态为“[name] => Array()”,我不知道可能是什么问题,因为这表明它们确实是数组,因此可能有东西被推入。还有 var $如上所述,当前始终具有正确的内容。在:
echo "current: ". $current;
完整代码:
<?php
$typedict = array();
$xsdstring = file_get_contents("infile.xsd");
$xsdstring = str_replace("xs:choice", "xs:sequence", $xsdstring);
$doc = new DOMDocument();
$doc->loadXML(mb_convert_encoding($xsdstring, 'utf-8', mb_detect_encoding($xsdstring)));
$xpath = new DOMXPath($doc);
$xpath->registerNamespace('xs', 'http://www.w3.org/2001/XMLSchema');
$xpath->registerNamespace('vc', 'http://www.w3.org/2007/XMLSchema-versioning');
function outputFormat($indent, $elementDef)
{
echo "<div>" . $indent . $elementDef->getAttribute('name')
. " type:" . $elementDef->getAttribute('type')
. " min:" . $elementDef->getAttribute('minOccurs')
. " max:" . $elementDef->getAttribute('maxOccurs')
. "</div>\n";
}
function echoElements($indent = "", $elementDef, $evaluate, &$typedict)
{
global $doc, $xpath, $current;
if($indent == "")
{
$attribute_name = $elementDef->getAttribute('name');
$typedict[$attribute_name] = array();
$current = $attribute_name;
}else{
echo "current: ". $current;
$type = $elementDef->getAttribute('name');
array_push($typedict[$current], "value");
#$typedict[$current][0] = "value";
print_r($typedict[$current]);
}
outputFormat($indent, $elementDef);
$elementDefs = $xpath->evaluate($evaluate, $elementDef);
foreach($elementDefs as $elementDef)
{
echoElements($indent . " ", $elementDef, $evaluate);
}
}
$elementDefs = $xpath->evaluate("/xs:schema/xs:element");
foreach($elementDefs as $elementDef)
{
echoElements("", $elementDef, "xs:complexType/xs:sequence/xs:element", $typedict);
}
$elementDefs = $xpath->evaluate("/xs:schema/xs:complexType");
foreach($elementDefs as $elementDef)
{
echoElements("", $elementDef, "xs:sequence/xs:element", $typedict);
}
print_r($typedict);
?>
【问题讨论】:
-
什么是
$current? -
您不能使用
array_push()分配键 - 它只是将项目添加到数组的末尾。如果你想使用$current来分配一个键,为什么不简单地使用$typedict[$current] = 'your value';? -
因为它不是“键”而是目标数组的键。 $typedict["whatever"] 是一个数组。我要推入“值”的数组。即: (linebreak) typedict["whatever"][0] (linebreak) typedict["whatever"][1] (linebreak) ...
-
我不尝试分配键,键都很好我真的想将值推送到 $typedict["whatever"] 这是一个空数组。最后的打印确认它仍然是帖子中提到的空数组。
标签: php multidimensional-array associative-array array-push