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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:32:16+00:00 2026-06-08T07:32:16+00:00

I have two arrays that I’m comparing and I’d like to know if there

  • 0

I have two arrays that I’m comparing and I’d like to know if there is a more efficient way to do it.

The first array is user submitted values, the second array is allowed values some of which may contain a wildcard in the place of numbers e.g.

// user submitted values
$values = array('fruit' => array(
        'apple8756apple333',
        'banana234banana',
        'apple4apple333',
        'kiwi435kiwi'
        ));

//allowed values
$match = array('allowed' => array(
        'apple*apple333',
        'banana234banana',
        'kiwi*kiwi'
        ));

I need to know whether or not all of the values in the first array, match a value in the second array.

This is what I’m using:

// the number of values to validate
$valueCount = count($values['fruit']);

// the number of allowed to compare against
$matchCount = count($match['allowed']);

// the number of values passed validation
$passed = 0;

// update allowed wildcards to regular expression for preg_match
foreach($match['allowed'] as &$allowed)
{
    $allowed = str_replace(array('*'), array('([0-9]+)'), $allowed);
}

// for each value match against allowed values
foreach($values['fruit'] as $fruit)
{
    $i = 0;
    $status = false;
    while($i < $matchCount && $status == false)
    {
        $result = preg_match('/' . $match['allowed'][$i] . '/', $fruit);
        if ($result)
        {
            $status = true;
            $passed++;
        }
        $i++;
    }
}

// check all passed validation
if($passed === $valueCount)
{
    echo 'hurray!';
}
else
{
    echo 'fail';
}

I feel like I might be missing out on a PHP function that would do a better job than a while loop within a foreach loop. Or am I wrong?

Update: Sorry I forgot to mention, numbers may occur more than 1 place within the values, but there will only ever be 1 wildcard. I’ve updated the arrays to represent this.

  • 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-08T07:32:18+00:00Added an answer on June 8, 2026 at 7:32 am

    If you don’t want to have a loop inside another, it would be better if you grouped your $match regex.

    You could get the whole functionality with a lot less code, which might arguably be more efficient than your current solution:

    // user submitted values
    $values = array(
              'fruit' => array(
                  'apple8756apple',
                  'banana234banana',
                  'apple4apple',
                  'kiwi51kiwi'
                )
              );
    
    
    $match = array(
               'allowed' => array(
                  'apple*apple',
                  'banana234banana',
                  'kiwi*kiwi'
                )
              );
    
    $allowed = '('.implode(')|(',$match['allowed']).')';
    $allowed = str_replace(array('*'), array('[0-9]+'), $allowed);
    
    
    foreach($values['fruit'] as $fruit){
      if(preg_match('#'.$allowed.'#',$fruit))
        $matched[] = $fruit;
    }
    
    print_r($matched);
    

    See here: http://codepad.viper-7.com/8fpThQ

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

Sidebar

Related Questions

I have two arrays that are structured like this $array1 = Array ( [0]
I currently have two arrays that look like this: Swatches: Array ( [0] =>
I have two arrays that both look like this: Array ( [0] => Array
I have two arrays that I would like to compare and ultimately wind up
I have two arrays that I create like this: public int GameBoard[][] = new
Assume that I have two arrays as follow: $array1 = array(1, 3, 5); $array2
I'm trying to come up with a way combine two arrays that have different
Let's imagine that we have two arrays: $array_1 = array( '0' => 'zero', '1'
I have two arrays of values that I would like to combine - but
let's say I have two arrays like so: $array1 = array('A' => array( 'B'

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.