How can I write in PHP a function that modifies an array?

前端 未结 3 801
悲哀的现实
悲哀的现实 2021-02-14 04:33

I would like to have a function that takes an array as input and changes some values of the array (in my case the array is $_SESSION but I think it does not really maters).

相关标签:
3条回答
  • 2021-02-14 04:52
    function change_array() {
    
         global $x; /*this will tell the function to work on the array 'x' out of the function itself.*/
         $x[0] = 100;
    
    }
    
    0 讨论(0)
  • 2021-02-14 04:53

    what you mean its call : Passing by Reference

    its very simple like

    function changearray(&$arr){
         $arr['x'] = 'y';
    }
    

    you can call this like :

    changearray($_SESSION);
    
    0 讨论(0)
  • 2021-02-14 05:15

    The coding is like:-

    $_SESSION['index_1'] = 'value 1';
    $_SESSION['index_2'] = 'value 2';
    

    If you want to change the value for the index "index_2" to value "value 2 changed", then you just simply write:-

    $_SESSION['index_2'] = 'value 2 changed'; 
    

    Hope it helps.

    0 讨论(0)
提交回复
热议问题