开发者

SimpleXML data processing

I'm close to finishing a script which uses simplexml to process some data but I am stuck at the last.

Well, not stuck, but curious and stuck.

$tmp contains my XML (approx 750k) and the goal is to get it into a format for fputcsv (a parent array containing an array for each row).

The below works, but instead of a nice clean array that I can use fputcsv and send back to user there is a lot of overhead which I do not need for writing to a CSV file and which will create me problems no doubt with fputcsv. I have cleaned the output up into just a couple of header columns and just a couple of rows, but in reality there are 20 columns and hundreds of rows. Anyway to clean before fputcsv?

        // Process XML response into arrays for fputcsv
        $csv = array();
        $xml = simplexml_load_string($tmp);

        // Create array and loop each column for header row
        $header_row = array();
        foreach ($xml->RESPONSE->DATA->HEADER->COLUMN as $column) {
            $header_row[] = $column;
        }
        $csv[] = $header_row;

        // Create array and loop each column for header row

        foreach ($xml->RESPONSE->DATA->ROW as $row) {
            $data_row = array();
            foreach ($row->COLUMN as $datacolvalue){
                $data_row[] = $datacolvalue;
            }
            $csv[] = $data_row;
            unset($data_row);
        }

        // Output var to check for accuracy (later add fputcsv)
        var_dump($csv);

Output (print_r - can var_dump if required):

Array
(
    [0] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [0] => date
                )

            [1] => SimpleXMLElement Object
                (
                    [0] => name
                )
)

    [1] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [data_type] => text
                        )

                    [0] => Aug 01, 2011
                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] =&g开发者_JAVA百科t; Array
                        (
                            [data_type] => text
                            [id] => 8699636
                        )

                    [0] => bfxgbfgxbfbgxfsfsdf
                )

        )

        [2] => Array

        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [data_type] => text
                        )

                    [0] => Aug 01, 2011
                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [data_type] => text
                            [id] => 8699694
                        )

                    [0] => bfxgbfgxbfbgxf
                )

        )
)

Desired output (extrapolated for each additional column and removing @attributes):

array(
array("date","name"),
array("Aug 01, 2011","bfxgbfgxbfbgxfsfsdf"),
array("Aug 01, 2011","bfxgbfgxbfbgxf")
)


Try changing

foreach ($xml->RESPONSE->DATA->HEADER->COLUMN as $column) {

to

$columns = (array)$xml->RESPONSE->DATA->HEADER->COLUMN;
foreach ($columns as $column) {

That should convert the SimpleXML objects to simple arrays. Which should remove the "overhead". Do the same for foreach ($row->COLUMN as $datacolvalue){.


Answering my own question, because I probably didn't explain it well enough, but specifying a (string) got me to where I needed to be.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜