为什么多维数组的一个数组不同于多个数组?

时间:2013-01-04 04:37:57

标签: php arrays json multidimensional-array

我不确定我是否正确地说明了这一点,但基本上,我有两个阵列(见下文)。第一个数组只有一个“segment”数组,但在执行var_dump时,它会显示一个子数组(或根本没有)。

较大的数组有多个“segment”数组,在var_dump中,你可以看到数组(5)。

背景是我从json字符串中获取这两个数组,我在php中解码。

以下是数组: JSON:

{ "css": { "segment": { "type": "Body", "comments": "Launch Pad v4 theme.css", "rule": { "selector": "html", "declaration": [ { "property": "margin", "value": "0" }, { "property": "padding", "value": "0" }, { "property": "height", "value": "100%" }, { "property": "background", "value": "#fff url(body_bg.gif) repeat-x top" } ] } } } }

阵列:

    array(1) {
  ["css"]=>
  array(1) {
    ["segment"]=>
    array(3) {
      ["type"]=>
      string(4) "Body"
      ["comments"]=>
      string(23) " v4 theme.css"
      ["rule"]=>
      array(2) {
        ["selector"]=>
        string(4) "html"
        ["declaration"]=>
        array(4) {
          [0]=>
          array(2) {
            ["property"]=>
            string(6) "margin"
            ["value"]=>
            string(1) "0"
          }
          [1]=>
          array(2) {
            ["property"]=>
            string(7) "padding"
            ["value"]=>
            string(1) "0"
          }
          [2]=>
          array(2) {
            ["property"]=>
            string(6) "height"
            ["value"]=>
            string(4) "100%"
          }
          [3]=>
          array(2) {
            ["property"]=>
            string(10) "background"
            ["value"]=>
            string(34) "#fff url(body_bg.gif) repeat-x top"
          }
        }
      }
    }
  }
}

JSON:

{ "css": { "segment": [ { "type": "Body", "comments": "Launch Pad v4 theme.css", "rule": [ { "selector": "html", "declaration": [ { "property": "margin", "value": "0" }, { "property": "padding", "value": "0" }, { "property": "height", "value": "100%" }, { "property": "background", "value": "#fff url(body_bg.gif) repeat-x top" } ] }, { "selector": "body", "declaration": [ { "property": "font", "value": "12px Arial, Helvetica, sans-serif" }, { "property": "line-height", "value": "1.35", "comments": "Specify default line height as a pure number value (no px/em)...adjusting the value as desired. This will allow the line-height to flex depending on what size the user specifies their text within the rich text editor" }, { "property": "color", "value": "#333" } ] } ] }, { "type": "Fonts/Typography", "comments": "Fonts/Typography" }, { "type": "Header" }, { "type": "Content" }, { "type": "Footer" } ] } }

阵列

array(1) {
      ["css"]=>
      array(1) {
        ["segment"]=>
        array(5) {
          [0]=>
          array(3) {
            ["type"]=>
            string(4) "Body"
            ["comments"]=>
            string(23) "theme.css"
            ["rule"]=>
            array(2) {
              [0]=>
              array(2) {
                ["selector"]=>
                string(4) "html"
                ["declaration"]=>
                array(4) {
                  [0]=>
                  array(2) {
                    ["property"]=>
                    string(6) "margin"
                    ["value"]=>
                    string(1) "0"
                  }
                  [1]=>
                  array(2) {
                    ["property"]=>
                    string(7) "padding"
                    ["value"]=>
                    string(1) "0"
                  }
                  [2]=>
                  array(2) {
                    ["property"]=>
                    string(6) "height"
                    ["value"]=>
                    string(4) "100%"
                  }
                  [3]=>
                  array(2) {
                    ["property"]=>
                    string(10) "background"
                    ["value"]=>
                    string(34) "#fff url(body_bg.gif) repeat-x top"
                  }
                }
              }
              [1]=>
              array(2) {
                ["selector"]=>
                string(4) "body"
                ["declaration"]=>
                array(3) {
                  [0]=>
                  array(2) {
                    ["property"]=>
                    string(4) "font"
                    ["value"]=>
                    string(33) "12px Arial, Helvetica, sans-serif"
                  }
                  [1]=>
                  array(3) {
                    ["property"]=>
                    string(11) "line-height"
                    ["value"]=>
                    string(4) "1.35"
                    ["comments"]=>
                    string(216) "Specify default line height as a pure number value (no px/em)...adjusting the value as desired. This will allow the line-height to flex depending on what size the user specifies their text within the rich text editor"
                  }
                  [2]=>
                  array(2) {
                    ["property"]=>
                    string(5) "color"
                    ["value"]=>
                    string(4) "#333"
                  }
                }
              }
            }
          }
          [1]=>
          array(2) {
            ["type"]=>
            string(16) "Fonts/Typography"
            ["comments"]=>
            string(16) "Fonts/Typography"
          }
          [2]=>
          array(1) {
            ["type"]=>
            string(6) "Header"
          }
          [3]=>
          array(1) {
            ["type"]=>
            string(7) "Content"
          }
          [4]=>
          array(1) {
            ["type"]=>
            string(6) "Footer"
          }
        }
      }
    }

我的问题是当“段”中只有一个数组时,如何强制数组,或者为什么会发生这种情况?

编辑:

我要问的是,为什么它在较小的数组中,只有一个“段”数组,我对它进行计数,显示3.它显示“注释”,“类型”和数组“规则“这些都是”细分“的孩子。现在在更大的数组中,有超过1个“段”行,计数为5,每个“段”都有一个带有“注释”,“类型”和数组“规则”的数组,而不是直接子项。

0 个答案:

没有答案