這是示例代碼:array_push不插入值
//$pieces is an stdClass object which has 4 elements the foreach loops through
$arr = array();
foreach($pieces as $piece)
{
$piece->value = 1;
array_push($arr, $piece);
$piece->value = 3;
array_push($arr, $piece);
}
與問題是,它不使用第一array_push
,就像它不是在那裏,在我得到的結果:
Array
(
[0] => stdClass Object
(
[piece] = 3
)
[1] => stdClass Object
(
[piece] = 3
)
[2] => stdClass Object
(
[piece] = 3
)
[3] => stdClass Object
(
[piece] = 3
)
)
雖然應該有額外的4鍵[piece] = 1
。難道我做錯了什麼?
提示:你是否考慮過對單個項目使用'$ array [] = $ value'與'array_push'? – Marty
@Marty當然,結果是一樣的。 – Lucas
當然,閱讀更好,打字速度更快,效率更高(小規模)。 – Marty