SimpleXMLElement对象显示而不是默认键

时间:2014-08-14 17:09:25

标签: php arrays

我正在尝试为CSV导出构建一个数组,但无法弄清楚为什么我的数组只用一个字符串构建一个子数组。

任何帮助都将不胜感激。

PHP:

foreach($Entry as $list){
  $pushArray = array($list['firstEntry'], $list['secondEntry']);
  array_push($csv, $pushArray);
}

echo "<pre>";
print_r($csv);
echo "</pre>";

输出:

Array
(
    [0] => Array
        (
            [0] => Title1
            [1] => Title2
        )

    [1] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [0] => Test Entry 1
                )

            [1] => SimpleXMLElement Object
                (
                    [0] => Test Entry 1.1
                )

        )

    [2] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [0] => Test Entry 2
                )

            [1] => SimpleXMLElement Object
                (
                    [0] => Test Entry 2.1
                )

        )

    [3] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [0] => Test Entry 3
                )

            [1] => SimpleXMLElement Object
                (
                    [0] => Test Entry 3.1
                )

        )

)

我希望它看起来像这样:

Array
(
    [0] => Array
        (
            [0] => Title1
            [1] => Title2
        )

    [1] => Array
        (
            [0] => Test Entry 1
            [1] => Test Entry 1.1
        )

    [2] => Array
        (
            [0] => Test Entry 2
            [1] => Test Entry 2.1
        )

    [3] => Array
        (
            [0] => Test Entry 3
            [1] => Test Entry 3.1
        )    
)

1 个答案:

答案 0 :(得分:3)

$pushArray = array((string) $list['firstEntry'], (string) $list['secondEntry']);

如果您以这种方式使用SimpleXML元素,则可以将其转换为字符串类型