Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - array_push() & $array[] doesn't work?

Tags:

arrays

php

push

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';
    } 
like image 744
Wordpressor Avatar asked Mar 08 '12 21:03

Wordpressor


2 Answers

$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();
}
like image 162
Ray Avatar answered Sep 28 '22 18:09

Ray


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));
like image 26
Arash Milani Avatar answered Sep 28 '22 17:09

Arash Milani