Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there another way than passing by reference to do what I want
    text
    copied!<p>I have the following data structure:</p> <p>A <code>Contract</code> has an array <code>projects</code>, which can have X number of projects. Each <code>Project</code> has an array, <code>subProjects</code>, which contain the same <code>Project</code> type, so theoretically you could have an infinite tree of Project-SubProjects-Project...</p> <p>Anyway, each project has a unique ID, and I need to search for a given project AND make a modification to that project, starting at the top level, and then store the changed contract back to my session. Currently, I'm doing it via a recursive function that returns a reference to the project it finds, but the more I'm searching, the more it seems people don't like PHP references. I'm not sure why, could someone explain the problems? Is there a better way to do what I want?</p> <p>Some code:</p> <pre><code>// Get the associative array version of the contract (it's stored as JSON) $contract = json_decode($contract, true); if(array_key_exists('projects', $contract)) { $resultProject = &amp;$this-&gt;findProject($contract['projects'], $projectId); if($resultProject) { $resultProject[$inputData['propertyName']] = $inputData['value']; \Session::put('workingContract', json_encode($contract)); // return 200 } } // Return 404 /** * Performs a depth-first search to find a project. * * @param array $projects * @param $projectId * @return null */ private function &amp;findProject(array &amp;$projects, $projectId) { foreach($projects as &amp;$project) { if($project['_id']['$id'] == $projectId) { return $project; } if(array_key_exists('subProjects', $project)) { $result = &amp;$this-&gt;findProject($project['subProjects'], $projectId); return $result; } } $null = null; // TODO: shitty hack for inability to return null when function returns a reference. Need to rethink use of references in general. Is there another way??? return $null; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload