I read the answer to this question but it led to more questions: PHP variables reference and memory usage
In short, I have a project where I'm trying to loop through a multidimensional array to grab one value before continuing on with either the process or the loop. It'll be easier to explain in code:
// Demo variables
$this->content['alias1'] = ('object' => new someObject; 'info' => array('type' => 'abc'));
$this->content['alias2'] = ('object' => new someObject; 'info' => array('type' => 'def'));
$this->content['alias3'] = ('object' => new someObject; 'info' => array('type' => 'abc'));
... that's incredibly simplified, but it gets the point across. I want to loop through that and find all of the 'types' that are 'abc' - what's the easiest way to do it? I had something like this
foreach($this->content as $alias => $data) {
if ($data['info'] != 'abc') {
break;
}
// Do actual "abc" things
}
But I'm trying to write all of this code as memory-conscientious as I can. I feel as though there's probably a more efficient way to do this.
I was thinking that when the objects are loaded (this is essentially a config file), it would assign a reference to another variable, something like
$this->types->$type =& $this->content['alias1'];
for each. In the question referenced above, it said that PHP uses copy-on-write with references - if the reference is only read, never written, would this be an efficient way to be able to access the object? I was also thinking that maybe I could just store the array key name in the $this->types->$type
array.