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

  • SEARCH
  • Home
  • 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 7733111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:54:14+00:00 2026-06-01T06:54:14+00:00

I have an xml file populated with keys and once a particular key has

  • 0

I have an xml file populated with keys and once a particular key has been used it will be deleted from the xml file. The code below runs, but does not delete the key from the xml file. I’ve read here on SO that maybe putting an “&” in the foreach() will ensure that I’m working with the actual xml file data rather than the temporary data created by the foreach(), but all I get when I run this code is;

Fatal error: An iterator cannot be used with foreach by reference in ....

xml file

<proctor>
    <keys>
        <key>afvr3e</key>
        <key>578jyd</key>
        <key>hnr6rg</key>
        <key>890kg7</key>
        <key>hn3rgd</key>
    </keys>
</proctor>

php index file

$key = new ProctorKey('proctorKey.xml');
$key->DeleteKey('hnr6rg');

php class file

class ProctorKey
{
    private $file;
    public function __construct($file)
    {
        $this->file = $file;
    }
    public function DeleteKey($keyToDelete)
    {
        $xml = simplexml_load_file($this->file)or die("Error: Cannot create object"); 
        /* this throws a fatal error
        *  foreach($xml->keys as &key)
        */
        foreach($xml->keys->key as $key)
        {
            if($key==$keyToDelete)
            {
                unset($key); 
                print('found');
            }
        }
        return $this->FormatXML($xml->asXML());
    }
    private function FormatXML($data)
    {
        $dom = new DOMDocument('1.0');
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        $dom->loadXML($data);
        $dom->save($this->file);
        $response = ($dom)?true:false;
        return $response;
    }
}
  • 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-06-01T06:54:16+00:00Added an answer on June 1, 2026 at 6:54 am

    The snippet below helps in two keys ways:

    • it uses XPath to iterate over an array containing only the key element(s) that you wish to delete
    • it calls unset on the actual element rather than the $key variable (it’s a quirk of SimpleXMLElement being an iterable object too).

    Snippet

    foreach($xml->xpath("keys/key[.='$keyToDelete']") as $key)
    {
        unset($key[0]); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a datagridview populated with data from an .xml file. The data is
I have a ListView that is populated using an XML file. However, I want
I have an XML file from which I am parsing some content to display
I have a datagrid that is populated via XML file when the form loads.
I have this xml snippet as part of a xml file that will be
What do I have? I have a ListBox populated with items from an XML
I have the following XML file getting populated via list view. I can't get
I have a list of object properties read from xml file and want to
I have a listbox on a usercontrol which is populated by a xml file.
I have created a custom listview that is populated using a row.xml file. Each

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.