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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:57:08+00:00 2026-05-12T14:57:08+00:00

and thanks in advance for the help. Background – I am writing a PHP

  • 0

and thanks in advance for the help.

Background –
I am writing a PHP script that needs to figure out the destination the caller is trying to reach. Telephony prefixes are strings that identify a destination. For each call, the program must find the longest prefix that matches the string. For example the number 30561234567 would be matched by 305 but not by 3057 or 304. If 3056 existed it would be the preferred match.

After investigating several data structures, a tree in which each node stores a digit and contains pointers to the other 10 possible choices seems ideal. This could be implemented as an array of arrays, where the script could check 3, find an array there, then check 0 on that new array, find another array and so on until it finds a value instead of an array. This value would be the destination id (the output of the script).

Requirements –
Performance is absolutely critical. Time spent checking these prefixes delays the call, and each server has to handle large amounts of calls, so the data structure must be stored in memory. There are approximately 6000 prefixes at the moment.

Problem –
The script is run each time the server receives a call, so the data must be held in a cache server of some sort. After checking memcached and APC (Advanced PHP Cache), I decided to use APC because it is [much faster][3] (it is a local memory store)

The problem I have is that the array of arrays can become up to 10 arrays deep, and will be a very complex data structure that, if I add to the cache as an object, will take a long time to de-serialize.

However if I add every single array to the cache separately (with some logical naming structure to be able to find it easily like 3 for array 3, then 30 for array 30, 305 for the array that follows that patch etc…) I will have to fetch different arrays from the cache many times (up to 10 per call), making me hit the cache too often.

Am I going about this the right way? Maybe there is another solution? Or is one of the methods I have proposed superior to the other?

Thank you for you input,

Alex

  • 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-12T14:57:09+00:00Added an answer on May 12, 2026 at 2:57 pm

    Here is some sample code for an N-ary tree structure;

    class PrefixCache {
     const EOS = 'eos';
     protected $data;
    
     function __construct() {
      $this->data = array();
      $this->data[self::EOS] = false;
     }
    
     function addPrefix($str) {
      $str = (string) $str;
      $len = strlen($str);
    
      for ($i=0, $t =& $this->data; $i<$len; ++$i) {
       $ch = $str[$i];
    
       if (!isset($t[$ch])) {
        $t[$ch] = array();
        $t[$ch][self::EOS] = false;
       }
    
       $t =& $t[$ch];
      }
    
      $t[self::EOS] = true;
     }
    
     function matchPrefix($str) {
      $str = (string) $str;
      $len = strlen($str);
    
      $so_far = '';
      $best = '';
    
      for ($i=0, $t =& $this->data; $i<$len; ++$i) {
       $ch = $str[$i];
    
       if (!isset($t[$ch]))
        return $best;
       else {
        $so_far .= $ch;
        if ($t[$ch][self::EOS])
         $best = $so_far;
    
        $t =& $t[$ch];     
       }
      }
    
      return false; // string not long enough - potential longer matches remain
     }
    
     function dump() {
      print_r($this->data);
     }
    }
    

    this can then be called as

    $pre = new PrefixCache();
    
    $pre->addPrefix('304');
    $pre->addPrefix('305');
    // $pre->addPrefix('3056');
    $pre->addPrefix('3057');
    
    echo $pre->matchPrefix('30561234567');
    

    which performs as required (returns 305; if 3056 is uncommented, returns 3056 instead).

    Note that I add a terminal-flag to each node; this avoids false partial matches, ie if you add prefix 3056124 it will properly match 3056 instead of returning 305612.

    The best way to avoid reloading each time is to turn it into a service; however, before doing so I would measure run-times with APC. It may well be fast enough as is.

    Alex: your answer is absolutely correct – but not applicable to this question 🙂

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's much better to use NSCalendar for this. It's designed… May 13, 2026 at 10:18 am
  • Editorial Team
    Editorial Team added an answer You could check their User-Agent string. May 13, 2026 at 10:18 am
  • Editorial Team
    Editorial Team added an answer Here's one that works and is not ugly: ;WITH Time_CTE… May 13, 2026 at 10:18 am

Related Questions

and thanks in advance for the help. Background - I am writing a PHP
I'm learning Clojure and would like some advice on idiomatic usage. As part of
I'm going on a couple assumptions here: XPathDocument is not editable. XmlDocument is editable.
A little background on this error: The customer getting this error message in their
I know at first glance (due to the title) this looks like one of

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.