title: Passing/Returning references to object + changing object is not working
body: <p>I am using <a href="https://stackoverflow.com/a/1643438/86421">an answer</a> from <a href="https://stackoverflow.com/questions/1643431/how-to-get-random-value-out-of-an-array">How to get random value out of an array</a> to write a function that returns a random item from the array. I modified it to <a href="http://php.net/manual/en/language.references.pass.php" rel="nofollow noreferrer">pass by reference</a> and <a href="http://php.net/manual/en/language.references.return.php" rel="nofollow noreferrer">return a reference</a>. </p>
  
  <p>Unfortunately, it doesn't appear to work. Any modifications to the returned object do not persist. What am I doing wrong?</p>
  
  <p>I'm on PHP 5.4 if that makes a difference (Don't ask).</p>
  
  <pre><code>function &amp;random_value(&amp;$array, $default=null)
  {
      $k = mt_rand(0, count($array) - 1);
      $return = isset($array[$k])? $array[$k]: $default;
      return $return;
  }
  </code></pre>
  
  <p>Usage...</p>
  
  <pre><code>$companies = array();
  $companies[] = array("name" =&gt; "Acme Co",  "employees"=&gt; array( "John", "Jane" ));
  $companies[] = array("name" =&gt; "Inotech",  "employees"=&gt; array( "Bill", "Michael" ));
  
  $x = &amp;random_value($companies);
  $x["employees"][] = "Donald";
  var_dump($companies);
  </code></pre>
  
  <p>Output...</p>
  
  <pre><code>array(2) {
    [0] =&gt;
    array(2) {
      'name' =&gt;
      string(7) "Acme Co"
      'employees' =&gt;
      array(2) {
        [0] =&gt;
        string(4) "John"
        [1] =&gt;
        string(4) "Jane"
      }
    }
    [1] =&gt;
    array(2) {
      'name' =&gt;
      string(7) "Inotech"
      'employees' =&gt;
      array(2) {
        [0] =&gt;
        string(4) "Bill"
        [1] =&gt;
        string(7) "Michael"
      }
    }
  }
  </code></pre>
  
  <p>I even copied and pasted the examples from the documentation and none of those worked either. They all output <code>null</code>.</p>