PHP array_push()二维数组

时间:2016-12-05 17:39:18

标签: php multidimensional-array associative-array array-push

   array_push($typedict[$current], "value");

这里似乎没有做任何事情,我输出数组的关联数组($ typedict),但所有数组都是空的(array())。我用echo打印当前的关联索引,以确认它是正确的(它总是)。

由于最后的打印chelc状态" [name] =>阵列()"我不知道可能是什么问题,因为这表明它们确实是数组,因此可以推入内容。此外,所述的var $ current始终具有正确的内容。于:

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 . "&nbsp;&nbsp;&nbsp;&nbsp;", $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);
    ?>

1 个答案:

答案 0 :(得分:-1)

$ current 确实包含正确的值。从代码中可以看出,每次设置它时都会检查它。所以我对这一部分非常有信心。

想要更改$ current中的任何内容,因为它的行为与我想要的完全一样。

我的目标是在关联数组$ typedict的每个条目中都有一个数组。 例如:

$typedict["whatever"][0] = "value";
$typedict["whatever"][1] = "value";
...

$ current是$ typedict的索引而不是它的任何内容。所以在这种情况下,$ current包含字符串“whatever”和 not “value”。这应该是这样的。

编辑: 我想我弄明白了这个问题。但我不知道如何发生这种情况,因此无法修复它: $ typedict [“whatever”]似乎只在IF块中可见,因为如果在块内部进行了chekcked,则最后一个值是可以的。

我不知何故需要将$ typedict的整个结构声明为全局。不仅仅是基本数组(关联数组),还有其中的所有数组。但我以后才会知道密钥。

EDIT2:

绝对是一个可见的问题: 变化:

global $doc, $xpath, $current;

为:

global $doc, $xpath, $current, $typedict;

解决了它