Delete some informations in a json variable with php

后端 未结 1 861
花落未央
花落未央 2021-01-29 07:11

My problem is that i am parsing an XML file and this file contain some information that i don\'t want to exporting as JSON data. In my case i want a function that return the jso

1条回答
  •  抹茶落季
    2021-01-29 07:29

    Remove every attribute you don't want just before encoding json:

    public function ParseXML ($url) {
        $fileContents= file_get_contents($url);
        // Remove tabs, newline, whitespaces in the content array
        $fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
        $fileContents = trim(str_replace('"', "'", $fileContents));
        $myXml = simplexml_load_string($fileContents);
        //--------------
        unset($myXml['@attributes']);
        unset($myXml['channel']);
        unset($myXml['image']);
        //--------------
        $json = json_encode($myXml);
        return $json;
    }
    

    or if you only need the item:

    public function ParseXML ($url) {
        $fileContents= file_get_contents($url);
        // Remove tabs, newline, whitespaces in the content array
        $fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
        $fileContents = trim(str_replace('"', "'", $fileContents));
        $myXml = simplexml_load_string($fileContents);
        //--------------
        $json = json_encode($myXml['item']);
        return $json;
    }
    

    0 讨论(0)
提交回复
热议问题