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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:15:38+00:00 2026-05-10T22:15:38+00:00

This question is coded in pseudo-PHP, but I really don’t mind what language I

  • 0

This question is coded in pseudo-PHP, but I really don’t mind what language I get answers in (except for Ruby :-P), as this is purely hypothetical. In fact, PHP is quite possibly the worst language to be doing this type of logic in. Unfortunately, I have never done this before, so I can’t provide a real-world example. Therefore, hypothetical answers are completely acceptable.

Basically, I have lots of objects performing a task. For this example, let’s say each object is a class that downloads a file from the Internet. Each object will be downloading a different file, and the downloads are run in parallel. Obviously, some objects may finish downloading before others. The actual grabbing of data may run in threads, but that is not relevant to this question.

So we can define the object as such:

class DownloaderObject() {     var $url = '';     var $downloading = false;      function DownloaderObject($v){ // constructor         $this->url = $v;         start_downloading_in_the_background(url=$this->$url, callback=$this->finished);         $this->downloading = true;     }      function finished() {         save_the_data_somewhere();         $this->downloading = false;         $this->destroy(); // actually destroys the object     } } 

Okay, so we have lots of these objects running:

$download1 = new DownloaderObject('http://somesite.com/latest_windows.iso'); $download2 = new DownloaderObject('http://somesite.com/kitchen_sink.iso'); $download3 = new DownloaderObject('http://somesite.com/heroes_part_1.rar'); 

And we can store them in an array:

$downloads = array($download1, $download2, $download3); 

So we have an array full of the downloads:

array(   1 => $download1,   2 => $download2,   3 => $download3 ) 

And we can iterate through them like this:

print('Here are the downloads that are running:'); foreach ($downloads as $d) {     print($d->url . '\n'); } 

Okay, now suppose download 2 finishes, and the object is destroyed. Now we should have two objects in the array:

array(   1 => $download1,   3 => $download3 ) 

But there is a hole in the array! Key #2 is being unused. Also, if I wanted to start a new download, it is unclear where to insert the download into the array. The following could work:

$i = 0; while ($i < count($downloads) - 1) {     if (!is_object($downloads[$i])) {         $downloads[$i] = new DownloaderObject('http://somesite.com/doctorwho.iso');         break;     }     $i++; } 

However, that is terribly inefficient (and while $i++ loops are nooby). So, another approach is to keep a counter.

function add_download($url) {     global $downloads;     static $download_counter;      $download_counter++;     $downloads[$download_counter] = new DownloaderObject($url); } 

That would work, but we still get holes in the array:

array(   1  => DownloaderObject,   3  => DownloaderObject,   7  => DownloaderObject,   13 => DownloaderObject ) 

That’s ugly. However, is that acceptable? Should the array be ‘defragmented’, i.e. the keys rearranged to eliminate blank spaces?

Or is there another programmatic structure I should be aware of? I want a structure that I can add stuff to, remove stuff from, refer to keys in a variable, iterate through, etc., that is not an array. Does such a thing exist?

I have been coding for years, but this question has bugged me for very many of those years, and I am still not aware of an answer. This may be obvious to some programmers, but is extremely non-trivial to me.

  • 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. 2026-05-10T22:15:39+00:00Added an answer on May 10, 2026 at 10:15 pm

    Some good answers to this question, which reflect on the relative experience on the answerers. Thank you very much — they proved very educational.

    I posted this question nearly three years ago. In hindsight, I can see my knowledge in that area was severely lacking. The biggest problem I had was that I was coming from a PHP perspective, which does not have the ability to arbitrarily pop elements. Something the other answers to this question helped me to discover was that a fundamentally superior model is ‘linked lists’.

    For C, I wrote a blog post about linked lists which contains code samples (too numerous to post here) but would neatly fill the original question’s use case.

    For PHP, a linked list implementation appears here, which I have never tried, but imagine it would also be the right way to deal with the above.

    Interestingly, Python lists contain the pop() method which, unlike PHP’s array_pop(), can pop arbitrary elements and keep everything in order. For example:

    >>> x = ['baa', 'ram', 'ewe'] # our starting point >>> x[1]                      # making sure element 1 is 'ram' 'ram' >>> x.pop(1)                  # let's arbitrarily pop an element in the middle 'ram' >>> x                         # the one we popped ('ram') is now gone ['baa', 'ewe'] >>> x[1]                      # and there are no holes: item 2 has become item 1 'ewe' 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Do this instead: public enum MyKeys { Key1, Key2, Key3… May 11, 2026 at 11:38 pm
  • Editorial Team
    Editorial Team added an answer A rule would be considered: p {…} A selector in… May 11, 2026 at 11:38 pm
  • Editorial Team
    Editorial Team added an answer There are some different ways. Plain string methods: Dim left… May 11, 2026 at 11:38 pm

Related Questions

This question is coded in pseudo-PHP, but I really don't mind what language I
I recently had my first encounter with PHP (5) through a Drupal application for
I'm trying to write a piece of code that will do the following: Take
I am querying Active Directory via LDAP (from Java and PHP) to build a
Currently I'm programming a database class which makes a little bit use of PHP's

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.