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 7565209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:05:44+00:00 2026-05-30T14:05:44+00:00

I am trying to write a php script which will insert an object into

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

  • 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-30T14:05:45+00:00Added an answer on May 30, 2026 at 2:05 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write an php twitter script which will be run by
I am trying to write a bash or PHP script that will execute a
I am trying to write a PHP script which executes shell functions for reporting.
I'm trying to write a simple PHP script which automatically sets up new etherpads
I'm trying to write a php script that will generate a variety of new
I am trying to write a PHP script which executes a shell command (via
I am very new to PHP, trying to write a script which connects to
I am trying to write a PHP script that can upload multiple files. for($i=0;$i<count($_FILES['uploadimg']['name']);$i++){
I am trying to write a PHP script that uses the pdftk app to
I am trying to write a program in PHP which I had already written

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.