PHP - array_push () & $ array [] not working?

1. PHP function.

I created a validation function, here it is in a 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; 
}; 

      

It basically takes $ input and stores it as $ valid_input. When it receives a new $ input, it overwrites the old #valid_inpu, etc.

I want to create an additional $ valid_input [$ id] array that won't overwrite itself, but just insert new elements inside.

2. Array_push () which doesn't work.

So, the new updated code will look like this:

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 the comment above - this doesn't work, the input always overwrites the parameter, it creates an array, but it always only contains one element, which is erased with the new one (I think array_push should prevent this 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; 
}; 

      

However, it overwrites the old $ valid_input value instead of clicking the item.

Any ideas? Maybe there is something wrong with the code? This whole function calls the Wordpress callback for the register_setting () function , but I'm guessing it is mostly PHP related as the people at WPSE can't help me.

4. EDIT

This does exactly what I want, but why does step 3. not work?

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

      

+3


source to share


4 answers


$ valid_input [$ id] must be set to an array before treating it as one.

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

      

The same applies to the [] notation



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

      

To check if an array is declared like this:

if(!is_array($valid_input[$id]){
   $valid_input[$id] =  array();
}

      

+5


source


The point is that objects are passed as a reference. you need to clone objects before using array_push function. here is an example function that clones an object:

function DeepCopy($ObjectToCopy) {
    return unserialize(serialize($ObjectToCopy));
}

      



then you can use it this way

array_push($MyObjectsArray, DeepCopy($MyObject));

      

+4


source


Is it possible that you are trying to output a new value into an array with a key value that already exists? i would try to check for the existing key value in your array before trying to push a value / key pair to it. Example:

if ( !isset( $arr[ $key ] ) ) {
  $arr[ $key ] = $value;
} else {
  echo " duplicate key value ";
}

      

+1


source


Either the array_push()

variable used with the array add operator []

must actually be an array, or they won't work. Double check that whatever is in $valid_input[$id]

is an array before performing array operations on the variable. Check by running:

if (is_array($valid_input[$id])) {
    // your code
}

      

0


source







All Articles