0

I have a piece of PHP code which returns an object variable with XML structure as follows:

<food1>
    <name1>Belgian Waffles</name1>
    <prices1>
        <price1>$5.95</price1>
        <price2>$8.95</price2>
    </prices1>
    <description1>Two of our famous Belgian Waffles with plenty of real maple syrup</description1>
    <calories1>
            <AA>
                <A1>650</A1>
                <A2>652</A2>
                <A3>653</A3>
            </AA>
            <BB>
                <B1>750</B1>
                <B2>751</B2>
                <B3>752</B3>
            </BB>
    </calories1>
</food1>

How to write a foreach loop in PHP that produces a variable like $food1 from the above? Then my plan is to insert the $food1 value into a text field in a MySQL database to parse and process the $food1 array whenever is needed.

   $food1 = array( 
        "food1" => array
        (
        "name1" => Belgian Waffles,        
        "prices1" => array (
                            price1 => $5.95
                            price2 => $8.95
                            ),      
        "description1" => 'Two of our famous Belgian Waffles with plenty of real maple syrup',
        "calories1" => array
                (
                "AA" => array (
                              A1 => 650
                              A2 => 652
                              A3 => 653
                               ),
                "BB" => array (
                              B1 => 750
                              B2 => 752
                              B3 => 753
                               ),
                )
         );
3
  • Use the simplexml_load_string function. Commented Mar 11, 2015 at 3:20
  • 1
    $xml_string = file_get_contents( 'in3.xml' ); $xml = simplexml_load_string($xml_string); $json = json_encode($xml); $aFoods = json_decode( $json, TRUE ); Commented Mar 11, 2015 at 3:25
  • 1
    If you're loading an XML file, use simplexml_load_file instead. But close to @VladimirRamik's anser Commented Mar 11, 2015 at 3:28

1 Answer 1

2

One remark in your XML change this:

<name2>Belgian Waffles</name1>

to

<name1>Belgian Waffles</name1>

Try this code:

<?php

$xml_string = '<food1>
    <name1>Belgian Waffles</name1>
    <prices1>
        <price1>$5.95</price1>
        <price2>$8.95</price2>
    </prices1>
    <description1>Two of our famous Belgian Waffles with plenty of real maple syrup</description1>
    <calories1>
            <AA>
                <A1>650</A1>
                <A2>652</A2>
                <A3>653</A3>
            </AA>
            <BB>
                <B1>750</B1>
                <B2>751</B2>
                <B3>752</B3>
            </BB>
    </calories1>
</food1>';

$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);

$out = array($xml->getName() => json_decode($json, true));

print_r($out);

?>
Sign up to request clarification or add additional context in comments.

4 Comments

thanks Adrian for your answer. Your answer works very well. One question, if I save the $out into the database, shall I use a foreach loop to access the elements of the array whenever there is a need to retrieve the saved value from the table?
@Sami don't forget check my answer as correct ;-) Why you want save an array?
Sure I did. Because I'm planning to insert elements of $out into a different table in multiple rows after some data processing in next steps. Can I save an array and later retrieve it from the database into a foreach loop? Would you have a better suggestion?
I think that is better serialize the array in your $_SESSION and then unserialize make the loop and insert in your other page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.