在 foreach 中保存到数组中时发生了一些奇怪的事情。
我得到了意想不到的结果,所以我将这部分代码用于debuging。
$search_case = new stdClass();
$same_case = array ();
print_r($families);
foreach ($families as $key => $family) {
echo "<br/><br/>";
$same_case['aaaaa'.$key] = $this->search_benef->search_case($family, $search_case);
print_r($same_case);
}
问题在于,当我获得$ key = 1的函数结果时,它还会更改表中的数据为$ key = 0.要知道我为我的表创建了aaaaa0和aaaaa1名称。
Bellow是 print_r 结果:
第一次print_r 我有2个对象,IBRAHIM和MOHAMMAD
stdClass对象( [0] =&gt; stdClass对象([focal] =&gt; stdClass对象([name] =&gt; RENAS [surname] =&gt; IBRAHIM [case_number] =&gt ; 36343)[case] =&gt; 36343) [1] =&gt; stdClass对象([focal] =&gt; stdClass对象([name] =&gt; AZAD [surname] =&gt; MOHAMMAD [case_number] =&gt; 54944)[case] =&gt; 54944))
在foreach循环中的第二个print_r ,在第一个循环中我获得了IBRAHIM的结果,在第二个循环中获得了MOHAMMAD的结果,但是它删除了aaaaa中IBRAHIM的结果。数组( [aaaaa0] =&gt; stdClass对象([old] =&gt; stdClass对象([hotel] =&gt; stdClass对象([focal] =&gt; stdClass对象([id ] =&gt; 26 [姓名] =&gt; RENAS [姓] =&gt; IBRAHIM)))))
数组( [aaaaa0] =&gt; stdClass对象([old] =&gt; stdClass对象([hotel] =&gt; stdClass对象([focal] =&gt; stdClass对象([id ] =&gt; 22 [name] =&gt; AZAD [surname] =&gt; MOHAMMAD))[athens] =&gt; stdClass对象([focal] =&gt; stdClass对象([id] =&gt; 24 [name] = &gt; AZAD [surname] =&gt; MOHAMMAD)))) [aaaaa1] =&gt; stdClass对象([old] =&gt; stdClass对象([hotel] =&gt; stdClass对象([焦点] ] =&gt; stdClass对象([id] =&gt; 22 [名称] =&gt; AZAD [姓] =&gt; MOHAMMAD))[athens] =&gt; stdClass对象([focal] =&gt; stdClass对象([id ] =&gt; 24 [姓名] =&gt; AZAD [姓] =&gt; MOHAMMAD)))))
你能解释一下为什么会这样吗?哪个错误?
- 编辑 -
我更改了我的代码并删除了'search_case'功能,但我遇到了同样的问题。内容发生了变化。下面我粘贴没有该功能的新代码。
$aaaaa = array ();
foreach ($families as $key => $family) {
echo "<br/><br/>Family->Case Number: " . $family->case;
$db_lists = array('athens' => 'athensDB', 'hotel' => 'hotelDB');
foreach ($db_lists as $key => $db_list) {
$remoteDB = $this->ci->load->database($key, TRUE);
$query = 'SELECT tb.id as id, name, surname FROM beneficiaries tb
left join _map_family_status tmfs ON tmfs.id=tb.family_status
where asylum_case = ' . $family->case . ' AND family_status = 1 GROUP BY asylum_case';
$focal = $remoteDB->query($query)->row();
if ($focal) {
$search_case->old->$key->focal = $focal;
}
}
echo '<br/> '.$family->case.' <br/> ';
$aaaaa[$family->case] = $search_case;
echo "<pre>";
print_r($aaaaa);
echo "</pre>";
}
return $aaaaa;
但结果令人失望。 Array [36343]内容随第二个循环的内容而变化。下面我粘贴两个循环的“print_r”结果。
Family->Case Number: 36343
Array (
[36343] => stdClass Object (
[old] => stdClass Object (
[hotel] => stdClass Object (
[focal] => stdClass Object (
[id] => 26
[name] => RENAS
[surname] => IBRAHIM
)
)
)
)
)
Family->Case Number: 54944
Array (
[36343] => stdClass Object (
[old] => stdClass Object (
[hotel] => stdClass Object (
[focal] => stdClass Object (
[id] => 22
[name] => AZAD
[surname] => MOHAMMAD
)
)
[athens] => stdClass Object (
[focal] => stdClass Object (
[id] => 24
[name] => AZAD
[surname] => MOHAMMAD
)
)
)
)
[54944] => stdClass Object (
[old] => stdClass Object (
[hotel] => stdClass Object (
[focal] => stdClass Object (
[id] => 22
[name] => AZAD
[surname] => MOHAMMAD
)
)
[athens] => stdClass Object (
[focal] => stdClass Object (
[id] => 24
[name] => AZAD
[surname] => MOHAMMAD
)
)
)
)
)