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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:48:25+00:00 2026-06-08T08:48:25+00:00

I’m trying to program a utility that handles a large volume of data and

  • 0

I’m trying to program a utility that handles a large volume of data and memory is a factor. Unfortunately each time this set of loops I have runs, it eats apx. 14MB of memory because it is executed thousands of times, even with the unset() calls (and yes I’m aware they do not clean up memory entirely, kind of why I’m asking the question). I’m wondering if there is an easier way to do this. Current working code:

        $qr = array();
        foreach($XML->row as $row)
        {
         $ra = array();
         foreach($row as $key => $value)
         {
         $ra[$key] = $value[0];
         unset($key,$value);
         }
        $qr[] = $ra;
        unset($row,$ra);
        }
        unset($XML);
        return $qr;

Another attempt was to do this, but it lags out. Anybody know what I’m doing wrong?

        $qr = array();
        while(list(,$row) = each($XML->row))
        {
         $ra = array();
         while(list($key,$value) = each($row))
         {
         $ra[$key] = $value[0];
         unset($key,$value);
         }
        $qr[] = $ra;
        unset($row,$ra);
        }
        unset($XML);
        return $qr;

Basically in the first loop, I’m just trying to do a basic array/object iteration. In the 2nd loop, I’m trying to go through each array value and get the 1st element while maintaining object/array index association. It seems I originally wrote it like this due to it being the only thing that worked (because it’s looping through a SimpleXML Object). Any tips on speeding this thing up or figuring out how to make it not eat memory would be appreciated.

I’m looking for solutions for garbage collection or more efficient code. I do not plan on replacing SimpleXML as there is no need for it. More clearly, I’m looking for:

  • A way to iterate the SimpleXML object without needing to call the inner loop (which is only due to me doing $value[0]. Why is that necessary?
  • A way which is more efficient (either speed or memory-wise) for iterating through the data
  • 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-08T08:48:27+00:00Added an answer on June 8, 2026 at 8:48 am

    That’s not how you access data from a SimpleXML object. I see you are using index [0] to get the string contents of each part of the object and treating it as an array. It’s not an array, it’s an object. This is how you should access string data… Example: http://php.net/manual/en/simplexml.examples-basic.php#example-5095

    Something like this will do the trick:

        $qr = array();
        foreach($XML->row as $row)
        {
         $ra = array();
         $ra['name'] = $value->name;
         $ra['name2'] = $value->name2;
         //Add a line for each element name, etc...
    
        $qr[] = $ra;
        unset($row,$ra);
        }
        unset($XML);
        return $qr;
    

    It will also get rid of your inner loop and save you memory.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
I have some data like this: 1 2 3 4 5 9 2 6
I know there's a lot of other questions out there that deal with this
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

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.