0

この指定された配列を変換したい

array(
    'podcast' => array(
        (int) 0 => array(
            'Podcast' => array(
                'id' => '2',
                'xmlurl' => 'http://test2.com'
            )
        ),
        (int) 1 => array(
            'Podcast' => array(
                'id' => '4',
                'xmlurl' => 'http://test4.com'
            )
        )
    )
)

CakePHP 2.3.6 を使用してこの文字列に変換します。

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<opml version="2.0">
    <head></head>
    <body>
        <outline xmlUrl="http://test2.com" />
        <outline xmlUrl="http://test4.com" />
    </body>
</opml>

どうすればいいですか?ここに Docがあることは知っていますが、助けていただければ幸いです。

これは私がこれまでに持っているものです:

$new = array();
foreach($podcasts as $p):
    $pod['xmlurl'] = $p['Podcast']['xmlurl'];
endforeach;
$new['opml']['body']['outline'][]=$pod;
debug($new);

$xmlObject = Xml::fromArray($new);
$xmlString = $xmlObject->asXML();
debug($xmlString);

出力debug($xmlString):

'<?xml version="1.0" encoding="UTF-8"?>
<opml>
    <body>
      <outline>
        <xmlurl>http://test1.com</xmlurl>
      </outline>
    </body>
</opml>'
4

1 に答える 1

1

リンクされたクックブックの記事で説明されているように、CakePHP がそれを認識できるように、それをフォーマットに変換する必要があります。を使用@して属性を示し、letをインスタンスにXml::fromArray()戻してDOMDocumentに設定できるようにしDOMDocument::xmlStandaloneますtrue

これ:

$podcasts = array(
    'podcast' => array(
        array(
            'Podcast' => array(
                'id' => '2',
                'xmlurl' => 'http://test2.com'
            )
        ),
        array(
            'Podcast' => array(
                'id' => '4',
                'xmlurl' => 'http://test4.com'
            )
        )
    )
);

$new = array (
    'opml' => array (
        '@version' => '2.0',
        'head' => null
    )
);

foreach($podcasts['podcast'] as $p) {
    $new['opml']['body']['outline'][] = array (
        '@xmlurl' => $p['Podcast']['xmlurl']
    );
};

$dom = Xml::fromArray($new, array('return' => 'domdocument'));
$dom->xmlStandalone = true;

echo $dom->saveXML();

次の XML が生成されます。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<opml version="2.0">
    <head></head>
    <body>
        <outline xmlurl="http://test2.com"/>
        <outline xmlurl="http://test4.com"/>
    </body>
</opml>
于 2013-06-23T16:44:24.327 に答える