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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:06:40+00:00 2026-06-11T11:06:40+00:00

I want to compare two arrays, one is set by default and another by

  • 0

I want to compare two arrays, one is set by default and another by user input.

When I have set a boolean value only in the default so I want to make sure the user won’t use string or number. for instance, ‘truex’ or ‘1’ is not acceptable.

Below is my sample of code,

$default = array(
    "randomise"     =>  false,
    "id"            =>  null
);

$config = array(
    "randomise"     =>  truex
);

function process_array($default,$config)
{
    # Loop the array.
    foreach($default as $key => $value)
    {

        if ((filter_var($default[$key], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === NULL) && (filter_var($config[$key], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === NULL)) 
        {
            return 'true or false only';
        }


    }

    # Return the result.
    return $array;
}

print_r(process_array($default,$config));

but this code returns ‘true or false only’ even though the user provides the correct data type. how can I fix 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-11T11:06:41+00:00Added an answer on June 11, 2026 at 11:06 am

    Alright first up you need to check that $config contains the key in $default. [Asside: If $config is user supplied… they could never user-supply an object like that, perhaps you meant $config=array("randomize"=>"truex");.. unless by user supplied you mean some other developer as the user (not a web user)).

    Second of all $config['id'] will always fail the test as you’ve written it (because it’s not a boolean).

    So, I’m trying to guess at what you’re doing here but I think this is what you want…

    $default = array("randomise"=>false,"id"=>null);
    //Example input
    $config = array("randomise"=>"truex");
    
    function process_array($default,$config) {
       foreach($default as $key => $value) {
          if (is_bool($default[$key])
              && isset($config[$key])) {
             //Update the array with the filtered value
             $config[$key]=filter_var($config[$key],FILTER_VALIDATE_BOOLEAN, 
                                      FILTER_NULL_ON_FAILURE);
             if ($config[$key]===null)
                return '$key can be true or false only';
             }
          }
       }
        return $array;
    }
    print_r(process_array($default,$config));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to compare two arrays, one coming from a shoppingcart and the other
I have two arrays, @a and @b . I want to do a compare
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I want to compare two arrays and then order one of them based on
I have a simple application in which I want compare two arrays. function check()
I want to compare two times in VB.net: I have 1:42:21 PM and I
I want to compare two files (one file is located on local location and
I have two arrays which contain objects of assets, now I want to subtract
I have two cell arrays of strings, and I want to check if they
I want to compare two 1x3 arrays such as: if output[x][y] != [150,25,75] (

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.