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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:16:43+00:00 2026-05-31T02:16:43+00:00

1. PHP function. I’ve created validating function, here it is in shorter version: function

  • 0

1. PHP function.

I’ve created validating function, here it is in shorter version:

function my_function($input) {
   $settings = my_source(); // function taht outputs a long array

   foreach ($settings as $setting) {
      $id = $setting['id'];  
      $foo = $setting['foo'];
      $option = get_option('my_theme_settings');

        if($foo == "bar") {
           $valid_input[$id] = $input[$id];
        }  

   } 

   return $valid_input; 
}; 

Basically it takes $input and saves it as $valid_input. When it gets new $input it overwrites the old #valid_inpu and so on.

I want to create an additional $valid_input[$id] array that will not overwrite itself, but just push new elements inside.

2. Array_push() that doesn’t work.

So the new updated code will look like that:

function my_function($input) {
   $settings = my_source(); // function taht outputs a long array

   foreach ($settings as $setting) {
      $id = $setting['id'];  
      $foo = $setting['foo'];
      $option = get_option('my_theme_settings');

        if($foo == "bar") {
           $valid_input[$id] = $input[$id];
        } 

        else if($foo == "noupdate") { // it doesn't work
           $valid_input[$id] = array();        
           array_push($valid_input[$id], $input[$id]);
        } 

   } 

   return $valid_input; 
}; 

As mentioned in comment above – this doesn’t work, input always overwrites the option, it creates an array but it always contains only one element that is being erased with the new one (I guess array_push should prevent that behavior, right?).

3. The same happens with $array[] =

function my_function($input) {
   $settings = my_source(); // function taht outputs a long array

   foreach ($settings as $setting) {
      $id = $setting['id'];  
      $foo = $setting['foo'];
      $option = get_option('my_theme_settings');

        if($foo == "bar") {
           $valid_input[$id] = $input[$id];
        } 

        else if($foo == "noupdate") { // it doesn't work
           $valid_input[$id][] = $input[$id];
        } 

   } 

   return $valid_input; 
}; 

Still it overwrites the old value of $valid_input instead of pushing an element.

Any ideas? Maybe there’s something wrong with the code? This whole function a WordPress callback for function called register_setting(), but I guess it’s mostly PHP related as folks on WPSE can’t help me.

4. EDIT

This does exactly what I want, but why point 3. doesn’t work then?

    else if($foo == "noupdate") { // it doesn't work
       $valid_input[$id][] = 'something';
       $valid_input[$id][] = 'something_else';
       $valid_input[$id][] = 'something_else2';
    } 
  • 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-31T02:16:45+00:00Added an answer on May 31, 2026 at 2:16 am

    $valid_input[$id] needs to be set to array before you treat it as one.

    $valid_input[$id] = array();
    array_push(  $valid_input[$id], "some stuff");
    

    Same deal with [] notation

    $valid_input[$id] =  array();
    $valid_input[$id][] =  "some stuff";
    

    To check if the array has been declared, so this:

    if(!is_array($valid_input[$id]){
       $valid_input[$id] =  array();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a php function to add number of days to input date function
The PHP function exif_read_data() requires a filename for input, however I only have the
Here is a php function: mysqli_stmt_bind_param($statement, "s", ...variable amount of parameters...); It will accept
Are Php function parameters passed as references to object or as copy of object?
The PHP function tempnam is failing to create a temporary file in the directory
Which php array function should be used to match a string to the first
fileA.php: requires function.inc.php and uses x() from that include file fileB.php: requires fileA.php User
i've made this php function to resize picture , and i want to save
We use very simple php function to display time and date. <div class=datedata><?=date(l, F

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.