Php以标准方式从多维数组中获取数据

时间:2015-08-18 14:09:11

标签: php multidimensional-array

我在php中有一个数组看起来像这样。

using DomXmlDocument = Windows.Data.Xml.Dom.XmlDocument;

    public static class DocumentExtensions
    {
        public static XmlDocument ToXmlDocument(this XDocument xDocument)
        {
            var xmlDocument = new XmlDocument();
            using (var xmlReader = xDocument.CreateReader())
            {
                xmlDocument.Load(xmlReader);
            }
            return xmlDocument;
        }

        public static DomXmlDocument ToDomXmlDocument(this XDocument xDocument)
        {
            var xmlDocument = new DomXmlDocument();
            using (var xmlReader = xDocument.CreateReader())
            {
                xmlDocument.LoadXml(xmlReader.ReadOuterXml());
            }
            return xmlDocument;
        }

        public static XDocument ToXDocument(this XmlDocument xmlDocument)
        {
            using (var memStream = new MemoryStream())
            {
                using (var w = XmlWriter.Create(memStream))
                {
                    xmlDocument.WriteContentTo(w);
                }
                memStream.Seek(0, SeekOrigin.Begin);
                using (var r = XmlReader.Create(memStream))
                {
                    return XDocument.Load(r);
                }
            }
        }

        public static XDocument ToXDocument(this DomXmlDocument xmlDocument)
        {
            using (var memStream = new MemoryStream())
            {
                using (var w = XmlWriter.Create(memStream))
                {
                    w.WriteRaw(xmlDocument.GetXml());
                }
                memStream.Seek(0, SeekOrigin.Begin);
                using (var r = XmlReader.Create(memStream))
                {
                    return XDocument.Load(r);
                }
            }
        }
    }

我想从数组中获取值。为此,我正在尝试这样

Array
(
    [0] => Array
        (
            [source_language] => English
            [target_language] => German
            [document_name] => 1439441727_plugin (copy).docx

            [title] => plugin (copy).docx
            [product_group_name] => Article Rewrite
            [id] => 1
        )

    [1] => Array
        (
            [source_language] => English
            [target_language] => German
            [document_name] => 1439536258_plugin (copy).docx

            [title] => plugin (copy).docx
            [product_group_name] => Article Rewrite
            [id] => 10
        )

)

但在这里我必须制作两个循环来获取数据。那么还有其他任何一种 我可以用更标准的方式获取数据的方式?任何帮助和建议都会非常明显。感谢

2 个答案:

答案 0 :(得分:3)

我相信你不了解foreach($arrs as $key => $arr ){}循环的工作原理。

有两种方法可以使用foreach:

  • 喜欢这样:foreach($my_array as $arr) { var_dump($arr); }

  • 或者像这样:[source_language] => English [target_language] => German [document_name] => 1439536258_plugin (copy).docx [title] => plugin (copy).docx [product_group_name] => Article Rewrite [id] => 10

您现在拥有它的方式是访问行值而不是数组块。

您需要做的是:

foreach($my_array as $arr) {
    var_dump($arr['id']);
}

这将导致:

var i

然后你可以这样选择:

// Never ever use 'i' in the global scope it will cause all sorts of head aches
var i = 0;  // so wrong, please never do this in global scope. PLEASE!!!.

// Now you are starting the code without checking if everything is in place.. Dom could be busy???
// you are also adding to the global scope. Never add to the global scope unless you have been commanded by god him self.
(function rotateBG() {      
    var el = document.getElementById( 'switcher' );  // should not be getting an element every time you need it. Get it once only
    var images = [  // over 300 bytes to store a number you are already tracking.. This has to go.
        'http://placehold.it/300x300&text=1',
        'http://placehold.it/300x300&text=2',
        'http://placehold.it/300x300&text=3',
        'http://placehold.it/300x300&text=4'
        ];

    // appart from the array access all good.
    el.style.backgroundImage = 'url(' + images[ i ] + ')';

    // very slow way of doing a cycling counter;
    i++;
    if ( i === 4 ) {
      i = 0;
    }

    setTimeout( rotateBG, 1200 );

})();

答案 1 :(得分:1)

看看 array_walk_recursive http://php.net/manual/en/function.array-walk-recursive.php

  

(PHP 5)array_walk_recursive - 递归地应用用户函数   数组的每个成员

它实现了嵌套循环

array_walk_recursive($my_array, function($array_value) {
  var_dump($array_value);
});