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();
gilezni
  • 36
  • 4
  • 1
    In `second`, you need to declare the parameter as a ref parameter using `&$arr` – Chris Haas Nov 20 '21 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](https://www.php.net/manual/en/language.references.pass.php) using `function second($type, &$arr)`. – Will B. Nov 20 '21 at 14:08

0 Answers0