5

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';
    } 
3
  • So you pass more than one $setting with the same $id. Have you tried debug-outputting $id and $foo inside the loop to check if it's not actually the data that leads to this behavior. I thought of a scenario where your $valid_input[$id] gets filled and the last $setting has $foo == "bar" and replaces your array with $input[$id]. Some example data for $settings would be helpful. Commented Mar 8, 2012 at 21:34
  • Basti, well, I guess something went really wrong since every setting has different $id :> Commented Mar 8, 2012 at 21:54
  • is $input[$id] an array in the first place or some value? You're treating it as if it is an array. Or force it to be an array: $valid_input[$id] = array($input[$id]); Commented May 25 at 20:32

4 Answers 4

7

$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();
}
Sign up to request clarification or add additional context in comments.

7 Comments

But I'm setting them as arrays, at least in the first example, it doesn't change anything in both cases.
You're trying to use them as multi dimensional arrays... the internal arrays are not being declared. Try adding the array declaration before the push or assignment.
I'm sorry but I don't exactly get what you mean. You are right, these are internal arrays, but I can't declare $valid_input[$id] as an array before foreach loop because it will create an array for all cases. And declaring it right before push/merging does nothing.
Ray, still nothing, but check the update. Maybe, just maybe, there's something wrong with $input[$id]?
That is the most ridiculous thing I've ever heard. THANKS!
|
4

The thing is that objects are passed as reference. you need to clone the objects before using the array_push function. here is a sample function that will clone an object:

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

then you can use it this way

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

3 Comments

$valid_input[$id] = array_push($valid_input[$id],unserialize(serialize($input[$id]))); returns nothing.
@Wordpressor: You don't use array_push() like that - check the manual. array_push() returns an integer value of the number of elements that are now in the array. Doing it the way you did in your comment is just overwriting the array!
That did it! Thanks a lot! Although I just created a new object before assigning it and pushing it onto the array: $dataObject = new $className; $dataObject = ...; array_push($dataArray, $dataObject);
1

is it possible that you are trying to push a new value to the array with a key value that already exists? i would test for an 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 ";
}

2 Comments

b_dubb, nice try, but that's not the case :(
still i don't see that you are testing for an existing key element in your array. you should test for that $arr[ key ] before trying to push new values to that $arr
0

Either array_push() or a variable used with the array append operator [] need to actually be an array or these won't work. Double check that whatever is in $valid_input[$id] is an array before doing array operations on the variable. Check by doing:

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

1 Comment

Crontab, they're both arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.