【问题标题】:PHP array_push() 2-dimensional arrayPHP array_push() 二维数组
【发布时间】: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 . "&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);
    ?>

【问题讨论】:

  • 什么是$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


【解决方案1】:

$current确实包含正确的值。正如代码中所见,我几乎每次设置它时都会检查它。所以我对那部分很有信心。

我确实不想要更改 $current 中的任何内容,因为它的行为完全符合我的意愿。

我的目标是关联数组 $typedict 的每个条目中的一个数组。 示例:

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

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

编辑: 我想我找到了问题所在。但我不知道这是怎么发生的,因此无法解决: $typedict["whatever"] 似乎只在 IF 块内可见,因为如果在块内检查最后一个值是可以的。

不知何故,我需要将 $typedict 的整个结构声明为全局的。不仅是基本数组(关联数组),还有它里面的所有数组。但我只是后来才知道钥匙。

编辑2:

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

global $doc, $xpath, $current;

到:

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

解决了

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 2018-12-30
    • 1970-01-01
    • 2011-08-13
    • 2020-06-10
    • 1970-01-01
    • 2017-01-19
    • 2013-04-24
    • 2017-03-02
    相关资源
    最近更新 更多