PHP表单只将一个对象推送到数组

PHP表单只将一个对象推送到数组,php,arrays,object,Php,Arrays,Object,每当productEntry类的新对象被推送到数组$products时,最新的条目将覆盖最后一个条目 if (isset($_POST['formedit'])){ $newproduct = new productEntry; $newproduct->productid = count($products); $newproduct->name = $_POST['productnamefield']; $n

每当productEntry类的新对象被推送到数组$products时,最新的条目将覆盖最后一个条目

    if (isset($_POST['formedit'])){
        $newproduct = new productEntry;
        $newproduct->productid = count($products);
        $newproduct->name = $_POST['productnamefield'];
        $newproduct->weight = $_POST['productweightfield'];
        $newproduct->length = $_POST['productlengthfield'];
        $newproduct->width = $_POST['productwidthfield'];
        $newproduct->height = $_POST['productheightfield'];
        $products[] = $newproduct;
    }
这是提交表单一次后的输出:

productEntry对象([productid]=>0[name]=>1[weight]=>1[length]=>1[width]=>1[height]=>1]productEntry对象([productid]=>1[name]=>2[weight]=>2[width]=>2[productid]=>2[name]=>3[weight]=>3[height]=>3[height]=>3]productEntry对象([productid]=>3[name]=>4[weight]=>4[length]=>4[width]=>4[height]=>4)

如果我用不同的值再次提交表单


productEntry对象([productid]=>0[name]=>1[weight]=>1[length]=>1[width]=>1[height]=>1]productEntry对象([productid]=>1[name]=>2[weight]=>2[width]=>2[productid]=>2[name]=>3[weight]=>3[height]=>3[height]=>3]productEntry对象([productid]=>3[name]=>5[weight]=>5[length]=>5[width]=>5[height]=>5)

我想这可能是重新分配products数组的最后一行。尝试将最后一行更改为
$products[]+=$newproduct听起来好像有一个循环?问题是什么还不清楚。。。电流输出是多少?期望的输出是什么?输入是什么样的?或者之后是否涉及数据库?很抱歉没有澄清。有一个下拉选择,显示产品。函数getOptions($products){$options='';$num=count($products);for($i=0;$i<$num;$i++){$options.='.$products[$i]>name.';}返回$options;}除非将数据安全地保存到数据库(或
$\u会话
var),否则脚本执行后数据将丢失。