0

I am trying to write a php script which will insert an object into an array of objects which is originally in XML format. I need to insert the object at a specified index and then be able to re-write the xml file from which the data was pulled with the updated object. Here is the structure of my XML

<?xml version="1.0" encoding="UTF-8"?>
<Bars>
    <Bar>
        <BarName>Kam's</BarName>
        <bar_id>0</bar_id>
        <Bartenders>
            <Bartender>
                <fname>Max</fname>
                <lname>Vest</lname>
                <imageURL>http://uofi-bars.com/bartenderImages/maxvest.jpg</imageURL>
                <shift>2</shift>
            </Bartender>
        </Bartenders>
        <Events>
            <Event>
                <EventName>Kams event</EventName>
                <date>08/10/1989</date>
            </Event>
         </Events>
         <Specials>
            <Special>Kam's Special 1</Special>
            <Special>Kam's Special 2</Special>
         </Specials>
    </Bar>

So in other words, if I have a bartender who works at a bar with an id of bar_id = 0, I need to be able to insert that bartender into the array of bartenders for that particular bar.

I use the following php code to create the arrays from XML:

function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();

    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }

    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}
$xmlUrl = "Bars.xml"; // XML 
    $xmlStr = file_get_contents($xmlUrl);
    $xmlObj = simplexml_load_string($xmlStr);
    $arrXml = objectsIntoArray($xmlObj);
    print_r($arrXml);

I guess I just don't know how to refer to this array of objects within an array of objects... Any help would be greatly appreciated!

Thanks!

3

1 Answer 1

2

if you just replace your code:

$xmlUrl = "Bars.xml"; // XML 
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
print_r($arrXml);

with this:

$xmlUrl = "Bars.xml"; // XML 
$xmlStr = file_get_contents($xmlUrl);
$xml = new SimpleXMLElement($xmlStr);
$bartenders = $xml->xpath('//Bartenders');
$new_bartender = $bartenders[0]->addChild('Bartender');
$new_bartender->fname = 'test1';
$new_bartender->lname = 'test2';
$new_bartender->imgURL = 'http://test.com';
$new_bartender->shift = '0';
print_r($bartenders);

this should do the trick, just replace the values with appropriate values :) i hope this helps!!

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

5 Comments

Thanks for the help, what about if I wanted to first take the bar_id that I have, select the corresponding bar, then check the bartenders array for corresponding duplicates. If you look at the XML I posted, there are several more bar objects in the <Bars> tag
I think i'd need to do a query to find the right bar_id... then query again within that specific bar.
you could use xpath to obtain those node objects and check their values to apply logic for specific cases, if i'm understanding the follow up question correctly :) also please don't forget to check off as an 'answer' to your question
so say I have a bar_id of 1, how would I query to that location with xpath and then add the bartender?
here you go some additional code to answer your follow up question: $bar = $xml->xpath('//Bar[bar_id=1]'); $updated_bartender = $bar[0]->Bartenders->Bartender; $updated_bartender->fname = 'update_test'; print_r($updated_bartender); i hope this helps, don't forget to +1 the comment :P

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.