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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:35:56+00:00 2026-06-15T07:35:56+00:00

I would like to validate that an array has and only has "a", "b",

  • 0

I would like to validate that an array has and only has "a", "b", and "c" as associate keys, and that the values are either integers or either NULL or 0 (what ever is easier).

For instance, array('a'=>123,'b'=>'abc', 'd'=>321) should be converted to array('a'=>123,'b'=>0, 'c'=>0).

I can do something like the following, but it is a little difficult to read, and will become big if I don’t just have 3 elements but 300.

$newArr=array(
    'a' => (isset($arr['a'])) ? (int)$arr['a'] : 0,
    'b' => (isset($arr['b'])) ? (int)$arr['b'] : 0,
    'c' => (isset($arr['c'])) ? (int)$arr['c'] : 0
);

Another option is something like the following:

$newArr = array();
foreach (array('a','b','c') as $key)
{
    $newArr[$key] = (isset($arr[$key])) ? (int)$arr[$key] : 0;
}

I guess this works good enough, however, am curious whether there is some slick array converting function that I don’t know about that would be better.

  • 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-15T07:35:57+00:00Added an answer on June 15, 2026 at 7:35 am

    It is possible to re-write your function using a combination of:

    • array_intersect_key to remove extra keys
    • array_merge to add missing keys
    • array_map to change every thing to NULL or integer

    However, this only makes it complicated. The slickest way IMO is this:

    $test = array("a" => 123, "b" => "x", "d" => 123);
    $testcopy = array();
    foreach (array("a", "b", "c") as $key) {
        $testcopy[$key] = array_key_exists($key, $test)
            ? filter_var($test[$key], FILTER_VALIDATE_INT, array("options" => array("default" => NULL)))
            : NULL;
    }
    var_dump($testcopy);
    

    Output:

    array(3) {
        ["a"]=> int(123)
        ["b"]=> NULL
        ["c"]=> NULL
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to validate a form so that it checks if the user
I would like to validate IP addresses from a list that may contain incorrectly
I would like to validate an email using the RegexValidator, e.g. [RegexValidator(@^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$)] Which works
I would like to validate the DNF (Disjunctive normal form) which looks like this
I would like to validate and restrict newline/linefeed character. Valid XML: <root> <node>data data</node>
I am receiving this XML file which I would like to validate. Ideally I
I'm using jQuery Validate and would like to re-validate a group of fields whenever
I would like to create a regex to validate customer names. This would be
I would like to know how can we validate the Credit Card. We have
I would like to use a pre-commit hook in riak to validate the json

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.