0

I want to modify the array in the first function using the second function but it doesn't return the result where it should(first Function).

    function first(){
    $results = [
        'programs' => [],
        'events' => [],
    ];
    second('program', $results);
    print_r($results); // Don't get results here
}

function second($type,$arr){
    $type .= 's';
    if(array_key_exists($type,$arr)){
        array_push($arr[$type],'title');
    }
    print_r($arr); // Get results here
}
first();
2
  • 1
    In second, you need to declare the parameter as a ref parameter using &$arr Commented Nov 20, 2021 at 14:05
  • arrays are passed to functions as a copy. Your second function would need to return the array back to the first function $results = second('program', $results); and function second($type, $arr){ ... return $arr; } or pass the array by-reference using function second($type, &$arr). Commented Nov 20, 2021 at 14:08

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.