将 SimpleXMLElement 对象转换为数组

2024-02-20

我必须将 SimpleXMLElement 对象转换为数组。

数组如下:

Array
(
[0] => SimpleXMLElement Object
    (
        [Cell] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [Data] => admin
                    )

                [1] => SimpleXMLElement Object
                    (
                        [Data] => base
                    )

                [2] => SimpleXMLElement Object
                    (
                        [Data] => Books
                    )

                [3] => SimpleXMLElement Object
                    (
                        [Data] => simple
                    )

                [4] => SimpleXMLElement Object
                    (
                        [Data] => 2,4,276,280
                    )

                [5] => SimpleXMLElement Object
                    (
                        [Data] => ACKTINDD20
                    )

                [6] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [7] => SimpleXMLElement Object
                    (
                        [Data] => Tinkle Double Digest Assorted (Pack of 20) + FREE Customized Mug
                    )

                [8] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [9] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [10] => SimpleXMLElement Object
                    (
                        [Data] => /p/r/product-image_1__1.jpg
                    )

                [11] => SimpleXMLElement Object
                    (
                        [Data] => /p/r/product-image_1__1.jpg
                    )

                [12] => SimpleXMLElement Object
                    (
                        [Data] => /p/r/product-image_1__1.jpg
                    )

                [13] => SimpleXMLElement Object
                    (
                        [Data] => tinkle-double-digest-assorted-pack-of-20-free-customized-mug
                    )

                [14] => SimpleXMLElement Object
                    (
                        [Data] => tinkle-double-digest-assorted-pack-of-20-free-customized-mug
                    )

                [15] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [16] => SimpleXMLElement Object
                    (
                        [Data] => 1 column no container CMS DEFAULT
                    )

                [17] => SimpleXMLElement Object
                    (
                        [Data] => Product Info Column
                    )

                [18] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [19] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [20] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [21] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                                [0] =>  
                            )

                    )

                [22] => SimpleXMLElement Object
                    (
                        [Data] => Use config
                    )

                [23] => SimpleXMLElement Object
                    (
                        [Data] => Use config
                    )

                [24] => SimpleXMLElement Object
                    (
                        [Data] => No
                    )

                [25] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [26] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [27] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [28] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [29] => SimpleXMLElement Object
                    (
                        [Data] => Comics
                    )

                [30] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [31] => SimpleXMLElement Object
                    (
                        [Data] => Rajani Thindiath
                    )

                [32] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [33] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [34] => SimpleXMLElement Object
                    (
                        [Data] => Teens & above
                    )

                [35] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [36] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [37] => SimpleXMLElement Object
                    (
                        [Data] => <p>Best of Tinkle Double Digest Assorted (Pack of 20) + FREE Customised Mug<p>
<p>Make this August even more special! Now get a FREE Customized Coffee Mug with a Tinkle Double Digest Assorted Pack of 20! What could be better than coffee, comics and your friend's company! <p>
<p>Tinkle Double Digest is a 180-page comic book which is published on a monthly basis. It is a collection of the best stories from the past issues of Tinkle Magazine, Tinkle Digest and Tinkle Holiday Specials. You can find stories of all the classic Tinkle toons, the new Tinkle Magazine toons and the original Tinkle Digest toons in every issue of Tinkle Double Digest. <p>

<p>Terms and Conditions: <p>
<p>1. Apply coupon code during checkout to get the discount calculated. 
<p>2. Offer only valid on White Coffee Mugs and not on any other mug variants. 
<p>3. Coupon code valid till October 31, 2016.
<p>4. Coupon code can only be used once per user. 

<p>Product link: http://www.zoomin.com/in/whitecoffeemug<p>

<p>Here are the steps to place an order:
<p>1. Select the theme for the Mug that you wish to order for. You can 
either choose from the pre-designed themes such as Dad, Mom, Rakhi, Love etc 
or else you can choose to create a custom theme yourself. Once you have made 
your theme selection select and click on the "Create" tab. <p>
<p>2. You will then be directed to the product builder page to complete your creation. Once done preview your Project and add your creation to the Cart. <p>
<p>3. Enter the coupon code under the discount section and hit apply. The Discount will automatically be computed under your Cart Summary. Click on Continue Checkout. <p>
<p>4. Enter your shipping address and as it is going to be a Free order click on Continue Payment and your order will get placed. <p>

                    )

                [38] => SimpleXMLElement Object
                    (
                        [Data] => <span></span>
                    )

                [39] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [40] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [41] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [42] => SimpleXMLElement Object
                    (
                        [Data] => 2400.0000
                    )

                [43] => SimpleXMLElement Object
                    (
                        [Data] => 1999.0000
                    )

                [44] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [45] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [46] => SimpleXMLElement Object
                    (
                        [Data] => Disabled
                    )

                [47] => SimpleXMLElement Object
                    (
                        [Data] => No
                    )

                [48] => SimpleXMLElement Object
                    (
                        [Data] => Catalog, Search
                    )

                [49] => SimpleXMLElement Object
                    (
                        [Data] => None
                    )

                [50] => SimpleXMLElement Object
                    (
                        [Data] => English
                    )

                [51] => SimpleXMLElement Object
                    (
                        [Data] => Paperback
                    )

                [52] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [53] => SimpleXMLElement Object
                    (
                        [Data] => Yes
                    )

                [54] => SimpleXMLElement Object
                    (
                        [Data] => 2015
                    )

                [55] => SimpleXMLElement Object
                    (
                        [Data] => Amar Chitra Katha
                    )

                [56] => SimpleXMLElement Object
                    (
                        [Data] => No
                    )

                [57] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [58] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [59] => SimpleXMLElement Object
                    (
                        [Data] => No
                    )

                [60] => SimpleXMLElement Object
                    (
                        [Data] => 2016-07-26 00:00:00
                    )

                [61] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [62] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [63] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [64] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [65] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [66] => SimpleXMLElement Object
                    (
                        [Data] => 0.0000
                    )

                [67] => SimpleXMLElement Object
                    (
                        [Data] => 0.0000
                    )

                [68] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [69] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [70] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [71] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [72] => SimpleXMLElement Object
                    (
                        [Data] => 1.0000
                    )

                [73] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [74] => SimpleXMLElement Object
                    (
                        [Data] => 0.0000
                    )

                [75] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [76] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [77] => SimpleXMLElement Object
                    (
                        [Data] => 2016-09-09 06:15:06
                    )

                [78] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [79] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [80] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [81] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [82] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [83] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [84] => SimpleXMLElement Object
                    (
                        [Data] => 0.0000
                    )

                [85] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [86] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [87] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [88] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [89] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [90] => SimpleXMLElement Object
                    (
                        [Data] => Tinkle Double Digest Assorted (Pack of 20) + FREE Customized Mug
                    )

                [91] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [92] => SimpleXMLElement Object
                    (
                        [Data] => simple
                    )

                [93] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [94] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

            )

    )

)

要将其转换为数组,我使用以下函数

function xmlToArray ( $xmlObj, $output = array () )
   {      
   foreach ( (array) $xmlObj as $index => $node )
   {
    $output[$index] = (is_object($node)) ? xmlToArray($node): $node;
   }
  return $output;
}

但通过这个我没有得到正确的数组,它给出了 [Cell] 元素作为 SimpleXMLElement 对象。

请为此提出一些解决方案。


我正在使用以下代码片段从 SimpleXMLElement 对象数组中提取值

array_flatten( json_decode ( json_encode( $simple_xml_object_var ), true ))

该解决方案生成一个平面值数组

Array
(
    [0] => Analyst Color
    [1] => Penny Stocks
    [2] => Short Sellers
    [3] => Analyst Ratings
)
    

这是 $simple_xml_object_var 的结构

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [is_array] => true
        )

    [item] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [name] => Analyst Color
                )

            [1] => SimpleXMLElement Object
                (
                    [name] => Penny Stocks
                )

            [2] => SimpleXMLElement Object
                (
                    [name] => Short Sellers
                )

            [3] => SimpleXMLElement Object
                (
                    [name] => Analyst Ratings
                )

        )

)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 SimpleXMLElement 对象转换为数组 的相关文章

  • yii2 更新后返回上一页

    更新记录后如何将用户重定向到上一页 这是典型的场景 用户在索引页面中过滤结果或对记录进行分页 然后找到想要编辑的记录并单击编辑按钮 他们更新该记录的数据 一旦单击 更新 按钮 他们将被重定向到索引视图 但具有先前选择的过滤器 页面 更新后我
  • 选择 mysql 枚举的 php 函数

    因此 我创建了一个函数 它将从数据库中的枚举字段中提取值
  • PHP 内部:TSRMLS_FETCH 如何工作?

    PHP 内部结构是怎样的TSRMLS FETCH宏发挥作用吗 Per the PHP手册 http php net manual en internals2 memory tsrm php 在开发扩展时 包含 tsrm ls is unde
  • PHP Laravel 路由问题

    我的设置目前看起来像这样 应用程序 控制器 register php class register Controller extends Base Controller public restful true public function
  • Volley Json 请求不起作用 - 字符串无法转换为 JsonObject / JsonArray

    我正在开发一个 Android 应用程序并从服务器获取 JsonObject JsonArray 手动将 String 转换为 Json 可以正常工作 我最近切换到 Volley 来处理服务器请求 并且想使用JsonObjectReques
  • 无法在 PHP 中使用 SFTP

    我正在用 PHP 编写一个简单的 SFTP 客户端 因为我们需要通过 n 个远程服务器以编程方式检索文件 我正在使用 PECL SSH2 扩展 不过 我遇到了障碍 php net 上的文档建议您可以这样做 stream fopen ssh2
  • 如何在加载ajax内容和javascript时加载gif图像[重复]

    这个问题在这里已经有答案了 我一直在尝试加载 gif 图像 直到 ajax 加载数据并显示它 但我对此感到安慰 我希望你能帮助我 这是我的高级搜索代码 现在我想为此添加加载 gif ajax type POST url base rul s
  • 使用 PHP 显示 Mysql 中的图像

    这就是我的数据库中的表的样子 我正在尝试显示我存储的图像 它是 mimetype longblob 当我运行代码时 它会给我一个带有 的小框 没有错误 只是那个框 有谁知道错误是什么以及如何修复它 Display Index Display
  • 在应用程序上按下类似按钮时自动刷新时间线页面?

    在时间轴发布到页面上之前 当用户在页面上按下 点赞 按钮时 页面会自行刷新 现在 启用时间线后 如果您访问应用程序并按右上角的类似按钮 页面不会自行刷新 并且应用程序不会识别用户 喜欢 该页面 当用户按下 喜欢 按钮时 是否有一种方法或事件
  • Node + now.js + 模型-视图-控制-模式

    我正在使用基于 MVC 模式 模板和 PHP 类 的论坛软件 页面如下所示 domain com index php page Test 我想使用 Node 和 now js 在一个页面 domain com index php page
  • 如何使用 MySQL 和 PHP 在数据库中存储标签?

    我想创建一个数据库来存储用户为其问题输入的标签 然后为发布的每个单独问题显示所有标签 像这里这样的东西 这是现在为我做所有事情的表 CREATE TABLE questions tags id INT UNSIGNED NOT NULL A
  • Python:在任意维度中使用冒号运算符对数组进行索引

    我有一个 numpy nd 数组 我的任务的简化版本是沿着每个轴获取一个向量 为了显示 import numpy x numpy array range 24 reshape 2 3 4 x0 x 0 0 x1 x 0 0 x2 x 0 0
  • 重新排列 numpy 数组

    我正在尝试 就地 修改 numpy 数组 我感兴趣的是就地重新排列数组 而不是 return ing 数组的重新排列版本 这是一个示例代码 from numpy import def modar arr arr arr 1 0 commen
  • PHP登录然后重定向

    我使用以下代码将用户登录到一系列安全页面 我需要将每个用户在提交后重定向到适当的页面 我想知道需要采取哪些步骤来选出三个登录级别 管理员 特殊 用户 if isset SESSION username function check logi
  • 获取特定 $_POST 数组的值

    name qty foreach POST as items gt value check qty gt 1 echo key gt value br 如何仅显示其值为 qty1 gt value qty2 gt value gt 0 的项
  • numpy 数组最快的保存和加载选项

    我有一个生成二维的脚本numpy数组与dtype float和形状的顺序 1e3 1e6 现在我正在使用np save and np load对数组执行 IO 操作 然而 这些函数对于每个数组都需要几秒钟的时间 是否有更快的方法来保存和加载
  • 如何在Redis中正确存储图片?

    决定将图像存储在Redis中 如何正确执行 现在我这样做 redis gt set image path here is the base64 image code 我不确定这是否正常 将图片存储在Redis中是完全可以的 Redis 键和
  • 未找到“Twilio\Rest\Client”类

    我正在尝试使用 twilio php api 这是我的代码
  • 检测图像是否损坏或损坏

    我需要以编程方式检查用户在我的应用程序上选择作为壁纸的图像是否已损坏或损坏 基本上我为用户提供了选择自己的图像作为壁纸的选项 现在 当图像加载时 我只想检查它是否已损坏 如果您正在寻找 PHP 解决方案而不是 javascript 解决方案
  • 实现 PHP 单例:静态类属性还是静态方法变量?

    所以 我总是像这样实现一个单例 class Singleton private static instance null public static function getInstance if self instance null se

随机推荐

  • firestore:权限缺失或不足

    我在登录时使用角色 允许读取 写入 if request auth uid null 我获取数据没问题 但是当我注销用户时 我收到错误 缺少权限或权限不足 首先我认为这是因为我没有取消订阅我尝试过的 Observable rxjs oper
  • Android Renderscript Allocation.USAGE_SHARED 崩溃

    我在运行使用渲染脚本的应用程序时发生崩溃 不幸的是 logcat 没有给出任何具体细节 b Bitmap createBitmap ib getWidth ib getHeight ib getConfig Allocation mInAl
  • Prettier 弄乱了 jsx 片段

    function App return lt gt lt Navbar gt lt Users gt gt 按 ctrl 将更改保存到 function App return lt gt lt Navbar gt lt Users gt l
  • 将 Python 解释器历史记录导出到文件?

    很多时候 在实际写入文件之前 我会使用 Python 解释器来检查变量并逐步执行命令 然而到最后 我的解释器中有大约 30 个命令 并且必须将它们复制 粘贴到文件中才能运行 有没有办法可以将 Python 解释器历史记录导出 写入到文件中
  • 在 SwiftUI 中,只有当用户将手指放在屏幕上时,什么手势才能执行代码?

    什么手势只有当用户将手指放在屏幕上时才能执行代码 运行某些代码不应该是原因 手势 的影响 我想要的是当用户将手指放在屏幕上时运行某些代码 如果用户将手指移开 代码就会停止运行 例如 some view unknownGesture runn
  • 将假设中的 ~exists 转换为 forall

    我陷入了假设的境地 exists k k lt n 1 f k f n 2 并希望将其转换为等效的 我希望如此 假设forall k k lt n 1 gt f k lt gt f n 2 这是一个小例子 Require Import Co
  • Airflow BashOperator 日志不包含完整输出

    我遇到一个问题 BashOperator 没有记录 wget 的所有输出 它只会记录输出的前 1 5 行 我已经尝试过仅使用 wget 作为 bash 命令 tester BashOperator task id testing bash
  • Visual Studio 2012 asp net mvc 3 项目剃刀视图

    我在新的 Visual Studio 2012 RC 中的 asp net mvc 3 项目中遇到智能感知问题 如果我在VS2012中打开VS2010 SP1 asp net mvc项目 cshtml视图如下所示 model Intrane
  • Windows 10 上的 Visual Studio 2012 中缺少“添加域服务类”

    我有一个 Silverlight 5 应用程序 需要对其进行维护工作 我想避免使用虚拟机 随着 Net Framework 4 6 2 的发布 目前处于预览版 this bug https connect microsoft com Vis
  • C/C++ 编译器是否将按二次方值进行常量除法优化为移位?

    问题说明了一切 有谁知道以下是否 size t div size t value const size t x 64 return value x 优化为 size t div size t value return value gt gt
  • 在 Android 的自定义适配器中设置 Textview 文本时出现问题

    我无法在其中设置 textview 的 setText 属性getView 自定义适配器的方法 我已经尝试过以下解决方案 但它对我不起作用 解决方案一 https stackoverflow com questions 5612844 ho
  • 在 ubuntu 16.04 中安装 Caffe 时遇到困难

    操作系统 ubuntu 16 04 CUDA 7 5 库德恩 5 我正在关注this https github com BVLC caffe wiki Ubuntu 16 04 or 15 10 OpenCV 3 1 Installatio
  • CSS,粘性页脚

    我知道 我知道 我以前问过这个问题 不幸的是 我失去了工作 我重新开始 回到原来的岗位 min height 100 在我的容器上不起作用 https stackoverflow com questions 9860888 min heig
  • std::remove 和 std::remove_if 设计的稳定性是否失败?

    最近 从一条评论中 我了解到std remove and std remove if是稳定的 我是否错误地认为这是一个糟糕的设计选择 因为它阻止了某些优化 想象一下删除 1M 的第一个和第五个元素std vector 由于稳定性原因 我们无
  • ie8 中未调用图像上的 onload 回调

    我正在尝试预加载图像并将高度和宽度设置为容器 问题似乎出在 ie8 中的缓存 因为它无法在后续刷新时加载 我查找并尝试了多种解决方案 但似乎没有任何效果 至少不一致 当前的 JavaScript img new Image img src
  • Pandas:将掩码应用于多索引数据帧

    我有一个带有 MultiIndex 列的 pandas 数据框 有 3 个级别 import itertools import numpy as np def mklbl prefix n return s s prefix i for i
  • UISegmentedControl 行为

    关于 iPhone 上的 UISegmentedControl 类的一个简单问题 希望有些人可能已经注意到 在具有 2 个分段的默认状态下 即使用户点击当前选定的分段 分段控件仍然会切换 我在应用程序中看到 UISegmentedContr
  • 如何安排对 MS Access 宏的调用?

    我正在寻找安排对 MS Access 宏的调用 该宏使用 csv 文件 我每天都会收到 来更新 SharePoint 日历 有人可以解释一下如何安排每日调用来在我的 Windows 7 计算机上运行此宏 我使用的是 Access 2003
  • 如何在 CodeIgniter 中获取会话超时?

    我试图在会话超时前 5 分钟运行一个函数 我的配置文件中的会话超时设置为 7 200 可以用 CodeIgniter 做到这一点吗 我想你正在寻找这样的东西 lastActivity this gt session gt userdata
  • 将 SimpleXMLElement 对象转换为数组

    我必须将 SimpleXMLElement 对象转换为数组 数组如下 Array 0 gt SimpleXMLElement Object Cell gt Array 0 gt SimpleXMLElement Object Data gt