Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6172669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:26:01+00:00 2026-05-23T23:26:01+00:00

I am trying to get some data off my DB and then trying to

  • 0

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>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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:26:02+00:00Added an answer on May 23, 2026 at 11:26 pm

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get some data from a table, where the tag has no
I have sugar crm instance and i was trying to get some data from
I am trying to get some form data from POST method. Here's the code
I'm trying to get some json data in my application, but it won't come
I'm trying to get some values off a DB and then putting those values
i am trying to get some data, but i dont know how can i
I am trying to get some data relevant to a stored procedure (or function)
I'm trying to recursively get some data using connect by, which for each row
I have been trying all day to get some data properly formatted in a
I'm having some problems trying to get the code below to output the data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.