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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:36:24+00:00 2026-06-17T09:36:24+00:00

How would I improve this functions for that is: Searches an array for fields

  • 0

How would I improve this functions for that is:
Searches an array for fields names which either:

  • a) Exactly matches
  • b) beings with “_” check if string begins with
  • c) ends with “_” check if string ends with

E.g I have a list of column names:

array(
    'customer_name',
    'customer_lastname',
    'customer_streetname',
    'customer_dob',
    'system_modified'
)

And another array with formatting conditions:

array(
    '_dob' => 'date_dob',
    '_name' => 'varchar',
    'customer_name' => 'html_text',
    'system_' => 'required'
)

Results apply the conditions against the column names:

1. customer_name = html_text (exact matches have higher preference)
2. customer_lastname = varchar
3. customer_streetname = 
4. customer_dob = dob
5. system_modified = required

Current have this:

protected function matchPatterns($string) {
    $return = array();
    $parrerns = $this->_normaliseArrayItem($this->getPatterns());
    foreach ($parrerns as $match) {

        // If exact match
        if($string == $match) {
            $return[] = $match;
            break;
        // Else if begins with _ and ends with string.
        } elseif($string[0] == "_" && substr_compare($string, $match, -strlen($match), strlen($match)) === 0) {
            $return[] = $match;
        }

    } // end loop
    return $return;
}


/**
 * Return an array of validation patterns.
 *
 * @return string[]
 */
public function getPatterns() {
    return $this->_patterns;
}


/**
 * Returns an item as array rather than single item.
 *
 * @param string[] $data
 * @return string[]
 */
protected function _normaliseArrayItem($data) {
    if(!isset($data[0])|| !is_array($data)) {
        $tmp[] = $data;
        $data = $tmp;
    }
    return $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-17T09:36:25+00:00Added an answer on June 17, 2026 at 9:36 am
    foreach ($parrerns as $match => $format) {
        // If exact match
        if ($string == $match) {
            $return[] = $format;
            break;
        // Else if begins with _ and string ends with it.
        } elseif ($match[0] == "_" && substr_compare($string, $match, -strlen($match), strlen($match)) === 0) {
            $return[] = $format;
        // Else if ends with _ and string begins with it
        } elseif (substr($match, -1) == "_" && substr_compare($string, $match, 0, strlen($match)) == 0) {
            $return[] = $format;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read that you can do it, but would this really improve performance
Would be possible to extend this kind of layout so that I can have
I would like to improve one of my simple matlab functions. Is there any
I would like to improve the lambda-code generated for the assert OCaml 3.12.1 construct.
Does running multiple Solr shards on a single machine improve performance? I would expect
Would it be wiser to check if a particular element exists on the page?
Would like a for loop in jquery so that: For every hover_link: show hidden
Would love some opinions on this problem I'm trying to workout. I'm trying to
Eclipse 3.5 has a very nice feature to generate Java hashCode() functions. It would
Following my previous question regarding the rationale behind extremely long functions, I would like

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.