开发者

PHP Array to XML dilemma

开发者 https://www.devze.com 2023-03-22 13:45 出处:网络
I am trying to get some data off my DB and then trying to display it in XML and JSON. After I select everything from the db, I add everything to an array called $data. So depending on how I want it to

I am trying to get some data off my DB and then trying to display it in XML and JSON. After I select everything from the db, I add everything to an array called $data. So depending on how I want it to be displayed, I can simply json_encode() it or use some lib ( This One ) to convert to XML. Now my problem/question is that I am trying to form the output style within the array so all I have to do in encode it and output it.

So this is the array data

Array (

[0] => Array
    (
        [Key1] => value1
        [Key2] => value2
        [Key3] => value3
        [Key4] => value4
    )

[1] => Array
    (
        [Key1] => value1
        [Key2] => value2
        [Key3] => value3
        [Key4] => value4
    )

[2] => Array
    (
        [Key1] => value1
        [Key2] => value2
        [Key3] => value3
        [Key4] => value4
    ) 

)

and I want it to be come out in this form in XML and Json

<xml>
<result>
<key1>value1</key1>
<key2>value2</key2>
<key3>value3</key3>
<key4>value4</key4>
</result>

<result>
<key1>开发者_如何学C;value1</key1>
<key2>value2</key2>
<key3>value3</key3>
<key4>value4</key4>
</result>

<result>
<key1>value1</key1>
<key2>value2</key2>
<key3>value3</key3>
<key4>value4</key4>
</result>
</xml>

Now what I am trying to find a solution for a way to make those arrays with a key result so when I convert the array directly to json or xml, I dont have to make additional changes to make every result entry abide to the result tag.

Is there a way to do this? Adding every array to key result overrides all the entries and outputs only the last entry.

Thanks


Lucky you, I was just writing this kind of thing for myself... Basically you supply a list of elements you want to use, by default it will use they key/index. hope this helps.

<?PHP

class Serializer
{   
    private static function getTabs($tabcount)
    {
        $tabs = '';
        for($i = 0; $i < $tabcount; $i++)
        {
            $tabs .= "\t";
        }
        return $tabs;
    }

    private static function asxml($arr, $elements = Array(), $tabcount = 0)
    {
        $result = '';
        $tabs = self::getTabs($tabcount);
        foreach($arr as $key => $val)
        {
            $element = isset($elements[0]) ? $elements[0] : $key;
            $result .= $tabs;
            $result .= "<" . $element . ">";
            if(!is_array($val))
                $result .= $val;
            else
            {
                $result .= "\r\n";
                $result .= self::asxml($val, array_slice($elements, 1, true), $tabcount+1);
                $result .= $tabs;
            }
            $result .= "</" . $element . ">\r\n";
        }
        return $result;
    }

    public static function toxml($arr, $root = "xml", $elements = Array())
    {
        $result = '';
        $result .= "<" . $root . ">\r\n";
        $result .= self::asxml($arr, $elements, 1); 
        $result .= "</" . $root . ">\r\n";
        return $result;
    }
}

    $arr = Array (
    0 => Array
    (
        'Key1' => 'value1',
        'Key2' => 'value2',
        'Key3' => 'value3',
        'Key4' => 'value4',
    ),

    1 => Array
    (
        'Key1' => 'value1',
        'Key2' => 'value2',
        'Key3' => 'value3',
        'Key4' => 'value4',
    ),

    2 => Array
    (
        'Key1' => 'value1',
        'Key2' => 'value2',
        'Key3' => 'value3',
        'Key4' => 'value4',
    ),
);
?>

Example 1

echo Serializer::toxml($arr, "xml", array("result"));

    //output
<xml>
    <result>
        <Key1>value1</Key1>
        <Key2>value2</Key2>
        <Key3>value3</Key3>
        <Key4>value4</Key4>
    </result>

    <result>
        <Key1>value1</Key1>
        <Key2>value2</Key2>
        <Key3>value3</Key3>
        <Key4>value4</Key4>
    </result>
    <result>

        <Key1>value1</Key1>
        <Key2>value2</Key2>
        <Key3>value3</Key3>
        <Key4>value4</Key4>
    </result>
</xml>

Exmaple 2

echo Serializer::toxml($arr, "xml", array("result", "item"));

// output
<xml>
    <result>
        <item>value1</item>
        <item>value2</item>
        <item>value3</item>
        <item>value4</item>
    </result>

    <result>
        <item>value1</item>
        <item>value2</item>
        <item>value3</item>
        <item>value4</item>
    </result>
    <result>

        <item>value1</item>
        <item>value2</item>
        <item>value3</item>
        <item>value4</item>
    </result>
</xml>

Example 3

echo Serializer::toxml($arr, "xml", array(null, "item"));

// output
<xml>
    <0>
        <item>value1</item>
        <item>value2</item>
        <item>value3</item>
        <item>value4</item>
    </0>

    <1>
        <item>value1</item>
        <item>value2</item>
        <item>value3</item>
        <item>value4</item>
    </1>
    <2>
        <item>value1</item>

        <item>value2</item>
        <item>value3</item>
        <item>value4</item>
    </2>
</xml>


I don't think you would need to use a library for this.

echo("<xml>");
foreach($data as $k => $v) {
    echo("<result>");
    foreach($v as $i => $j)
        echo("<".$i.">" . $j . "</".$i.">");
    echo("</result>");
}
echo("</xml>");


Assuming your outer array is called $array use the [] syntax. This will give you an array key called 'result' which itself is an indexed array. When converted to XML, I believe (though have not tested) that its output will be what you are looking for.

$results = array('result' => array());

// Looping over the outer array gives you the inner array of keys
foreach ($array as $result) {
    // Append the array of keys to the results array
    $results['result'][] = $result;
}

print_r($results);
echo json_encode($results);

Now use your array to XML library to make the XML.


foreach ($data as $key => $value) {

                    //change false/true to 0/1
                    if(is_bool($value))
                    {
                            $value = (int) $value;
                    }

                    // no numeric keys in our xml please!
                    if (is_numeric($key))
                    {
                            // make string key...
                            $key = (singular($basenode) != $basenode) ? singular($basenode) : 'item';
                    }

                    // replace anything not alpha numeric
                    $key = preg_replace('/[^a-z_\-0-9]/i', '', $key);

                    // if there is another array found recursively call this function
                    if (is_array($value) || is_object($value))
                    {
                            $node = $structure->addChild($key);

                            // recursive call.
                            $this->to_xml($value, $node, $key);
                    }

                    else
                    {
                            // add single node.
                            $value = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, "UTF-8");

                            $structure->addChild($key, $value);
                    }
            }
0

精彩评论

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