1

PHP (simplexml) に xml パーサーがあり、xpath の下にインデックス要素を選択して保存したい

function startXML(&$resarrary, $path, $lang) {

if(file_exists($path)) {
    $xml = simplexml_load_file($path);
    if($xml) {      
        foreach($xml->xpath("//languageKey") as $cos) {
        utf8_encode($cos);

        // Select required languagekey 

        if ($cos[index] == $lang) {
        foreach($xml->xpath("//languageKey[@index='$lang']//label[@type='js']") as $cos) {
            ?>
                <table>
                    <tr>
                        <td><?php echo $cos ?></td>
                    </tr>
                </table>    
            <?php 
            array_push($resarrary,$cos);

            }

        } 

      }

    } 
    else {
        FALSE;
    }
}

}

テーブル echo $cos はテスト用です....xmlファイルからの正しい検索結果が得られます(この場合、結果は「モード1」です)

選択は機能しますが、結果 (「モード 1」) を配列に保存するにはどうすればよいですか? この array_push(...) を試して、resarray を表示するために print_r を使用すると、simplexml xpath 検索のコンテンツ全体しか取得できません

配列 ( [0] => SimpleXMLElement オブジェクト ( [@attributes] => 配列 ( [index] => list_mode_1 [type] => js ) [0] => モード 1 ) )

行 array_push($resarrary,$cos->attributes()); を変更すると 私はこれを得る

配列 ( [0] => SimpleXMLElement オブジェクト ( [@attributes] => 配列 ( [index] => list_mode_1 [type] => js ) ) )

4

1 に答える 1

0

XML

<lKey index="default" type="array">
    <a index="list_mode_1" type="js">Mode 1</a>
    <b index="list_mode_2">Mode 2</b>
    <c index="list_mode_3">Mode 3</c>
</lKey>
于 2012-08-02T08:45:01.653 に答える