0

I have a huge array in which keys are also not constant in most of the cases, but there are 3 keys that always constant (#name,#default_value,#value) and #default_value and #value is different i want to get these kind of sub arrays in 1 simple array , for this purpose i am using recursion in whole array and checking out if these 3 keys are present there i can print these values inside recursion easily but I am not able to get those values in return. So that i can precess them further.

$form=array();
$form['field_one']['#name']="field_one";
$form['field_one']['#default_value']="old";
$form['field_one']['#value']="new";
$form['field_two']['#name']="field_two";
$form['field_two']['#default_value']="old";
$form['field_two']['#value']="new";
$form['field_three']['#name']="field_three";
$form['field_three']['#default_value']="old";
$form['field_three']['#value']="old";
$form['thiscouldbeanotherarray']['idk']['field_four']['#name']="field_four";
$form['thiscouldbeanotherarray']['idk']['field_four']['#default_value']="old";
$form['thiscouldbeanotherarray']['idk']['field_four']['#value']="new";
$arr_get = get_updatedvalues($form,array());
var_dump($arr_get);

function get_updatedvalues($form,$fields) {  
    if (!is_array($form)) {
        return;
    }

    foreach($form as $k => $value) { 
        if(str_replace('#','',$k) =='default_value' && ($form['#default_value'] != $form['#value'] ) ){               
            $fields['field'][$form['#name']] = array("name" => $form['#name'],"old_value" =>$form['#default_value'],"new_value" =>$form['#value']);              
            var_dump($fields);
        }
        get_updatedvalues($value,$fields);     
    }   
    return $fields;
}

If you run this code it will give you $arr_get > array (size=0), there i need all three values
4
  • don't you think you should define the function before calling it? also, why are you calling the function inside itself? Commented May 26, 2021 at 13:59
  • 1
    its a recursive function. that checkouts elements of whole array, and checks if certain values are present in it. thats why i need recursive loop Commented May 26, 2021 at 14:05
  • the $form array is example, its not an actual one, that array is too big having 2000 elements Commented May 26, 2021 at 14:06
  • @AdisonMasih don't you think you should define the function before calling it ...that isn't required in PHP Commented May 26, 2021 at 14:08

1 Answer 1

1

If I understand correctly, you need to pass fields as a reference in order to change it:

function get_updatedvalues($form, &$fields) {

To do that, however, you'll need to change your initial call so that you have a variable to pass as the reference:

$array = [];
$arr_get = get_updatedvalues($form,$array);

Running this I get:

Array
(
    [field] => Array
        (
            [field_one] => Array
                (
                    [name] => field_one
                    [old_value] => old
                    [new_value] => new
                )

            [field_two] => Array
                (
                    [name] => field_two
                    [old_value] => old
                    [new_value] => new
                )

            [field_four] => Array
                (
                    [name] => field_four
                    [old_value] => old
                    [new_value] => new
                )

        )

)

Sign up to request clarification or add additional context in comments.

Comments

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.