在foreach循环中分配对象属性; 数据不会在foreach循环之外持续存在。 为什么不? INT和STRING分配确实有效

在foreach循环中分配对象属性; 数据不会在foreach循环之外持续存在。 为什么不?  INT和STRING分配确实有效

问题描述:

Been knocking my head off the desk on this one all day.

    // Iterate over project array to populate release data
    for ($i = 0; $i < count($data); $i++) {
        $data[$i]->setProjectReleaseSchedule( $proj_scheds[$i] );

            //Get each projects Est and Act hours
            $options = new stdClass;
            $options->default       = true;
            $options->project_id    = $data[$i]->getProjectId();
            $options->department_id = $person_dm->getPersonDepartmentId();

            //works; AKA: property get assigned an int thatr incriments evern loop
            //$data[$i]->data->proj_hours = $this->counter++;

            //$weekly_report_dm->getProjectHours returns an object w/ populated properties
            $data[$i]->data->proj_hours = $weekly_report_dm->getProjectHours($options);

            //As a test, this dumps what is expected...
            echo'<pre>';
            print_r( $data[$i]->data->proj_hours );
            echo'</pre>';

    }


echo'<pre>';
//After the loop completes and I try to dump the data the property $data->data->proj_hours is assigned an object..
//but the object properties are all null :(.
print_r( $data );
echo'</pre>';
exit;

So as you can see Im looping over an array of objects; for each object I got another method for data, it returns with expected data. The "print_r( $data[$i]->data->proj_hours );". However, once the loop finishes and I try to dump the data the object is assigned to the parent, but the properties of said object are null/blank.

So the question: Why can I assign INTs, STRINGS, etc to an objects property and they persist outside the foreach loop. But an objects properties when assigned to a parent object inside a foreach loop, the values do not persist?

整天都把我的头从桌子上敲了下来。 p>

   //迭代项目数组以填充发布数据
($ i = 0; $ i&lt; count($ data); $ i ++){
 $ data [$ i]  - &gt; setProjectReleaseSchedule($  proj_scheds [$ i]); 
 
 //获取每个项目的Est和Act小时
 $ options = new stdClass; 
 $ options-&gt; default = true; 
 $ options-&gt; project_id = $ data  [$ i]  - &gt; getProjectId(); 
 $ options-&gt; department_id = $ person_dm-&gt; getPersonDepartmentId(); 
 
 //有效;  AKA:属性被赋予一个int thatr incriments evern loop 
 // $ data [$ i]  - &gt; data-&gt; proj_hours = $ this-&gt; counter ++; 
 
 // $ weekly_report_dm-&gt; getProjectHours返回 一个具有填充属性的对象
 $ data [$ i]  - &gt; data-&gt; proj_hours = $ weekly_report_dm-&gt; getProjectHours($ options); 
 
 //作为测试,这会转储预期的内容。  .. 
 echo'&lt; pre&gt;'; 
 print_r($ data [$ i]  - &gt; data-&gt; proj_hours); 
 echo'&lt; / pre&gt;'; 
 
} 
  
 
echo'&lt; pre&gt;'; 
 //循环完成后我尝试转储数据属性$ data-&gt; data-&gt; proj_hours被赋予一个对象.. 
 //但是 对象属性都为null:(。
print_r($ data); 
echo'&lt; / pre&gt;'; 
exit; 
  code>  pre> 
 
 

所以你可以看到 我循环遍历一个对象数组;对于每个对象,我得到另一个数据方法,它返回预期数据。“print_r($ data [$ i] - &gt; data-&gt; proj_hours);”。但是,一旦 循环结束 我尝试转储对象分配给父对象的数据,但所述对象的属性为空/空。 p>

所以问题:为什么我可以分配INT,STRINGS等 到对象属性,它们在foreach循环之外持久存在。 但是当一个对象属性分配给foreach循环中的父对象时,这些值不会持久存在吗? p> div>

It seems that weekly_report_dm->getProjectHours($options) returns reference to same private (?) property , which changes each call. You can try to clone the result with clone :

$data[$i]->data->proj_hours = clone $weekly_report_dm->getProjectHours($options);

Changing getProjectHours to not return reference would be the right solution

On top, outside for loop add:

$filler = array();

Inside for loop add this:

$filler[] = $data[$i]->data->proj_hours;

Then try to dump data afterwards.

print_r($filler);

Hope that will bring you some results.