php while循环错误


php while loop error

我有一个PHP MySQL fetch while循环,如下脚本所示:

$result2211 = mysql_query("select * from products where is_config = 'yes' ");
while($row2211 = mysql_fetch_assoc($result2211))
{
   $sn = $row2211['sn'];
   $allparrentselectq = mysql_query("SELECT * FROM parrentpro where parrentsn = $sn");
   while($allparrentselect = mysql_fetch_assoc($allparrentselectq))
   {
       $childarr = unserialize($allparrentselect['childsn']);
       $subpro  = '{"catname":"'.$allparrentselect['childname'].'",';
       $i = 0;
       foreach($childarr as $childarr):
           $subpro .= '"Pro'.$i++.'":"'.$childarr.'",';
       endforeach;
       $subpro1[] = substr($subpro, 0, -1)."}";
       $subproa = "[".implode(",",$subpro1)."]";
   }
   $prodObj2 = new ProductDetails();
   $prodObj2->productname = $row2211['productname'];
   $prodObj2->price = $row2211['productprice'];
   $prodObj2->discount = $row2211['discount'];
   $prodObj2->discountprice = $row2211['discountprice'];
   $prodObj2->imageURL = $row2211['productimageurl'];
   $prodObj2->category = $row2211['productcat'];
   $prodObj2->configurablepone = $subproa;
   $prodObj2->configurable = 'yes';
   array_push($totArr, $prodObj2);
}

我有一个问题。我得到的结果如下(JSON):

[
    {
        "productname":"Veg.Pizaa",
        "price":"350",
        "discount":"",
        "discountprice":"350",
        "imageURL":"http:'/'/farm8.staticflickr.com'/7154'/6694188161_9ee692d854_s.jpg",
        "category":"Pizaa",
        "configurablepone":[{"catname":"Extra","Pro0":"Extra 25g Cheese","Pro1":"Extra 75g Cheese"},"catname":"Nuts","Pro0":"Almonds","Pro1":"Peanuts","Pro2":"Pistachios"}],
        "configurable":"yes"
    },
    {
        "productname":"Core i7 Pc",
        "price":"48000",
        "discount":"2",
        "discountprice":"47040",
        "imageURL":"http:'/'/www.4to40.com'/images'/science'/Basic_Computer_Parts'/Computer.jpg",
        "category":"Pc",
        "configurablepone":[{"catname":"Extra","Pro0":"Extra 25g Cheese","Pro1":"Extra 75g Cheese"},{"catname":"Nuts","Pro0":"Almonds","Pro1":"Peanuts","Pro2":"Pistachios"},{"catname":"Harddisk","Pro0":"Segate 500Gb","Pro1":"Samsung 250Gb"},{"catname":"Ram","Pro0":"8Gb Ram","Pro1":"4Gb Ram","Pro2":"2Gb Ram"}],
        "configurable":"yes"
    }
]

但我需要如下结果:

[
    {
        "productname":"Veg.Pizaa",
        "price":"350",
        "discount":"",
        "discountprice":"350",
        "imageURL":"http:'/'/farm8.staticflickr.com'/7154'/6694188161_9ee692d854_s.jpg",
        "category":"Pizaa",
        "configurablepone":[{"catname":"Extra","Pro0":"Extra 25g Cheese","Pro1":"Extra 75g Cheese"},"catname":"Nuts","Pro0":"Almonds","Pro1":"Peanuts","Pro2":"Pistachios"}],
        "configurable":"yes"
    },
    {
        "productname":"Core i7 Pc",
        "price":"48000",
        "discount":"2",
        "discountprice":"47040",
        "imageURL":"http:'/'/www.4to40.com'/images'/science'/Basic_Computer_Parts'/Computer.jpg",
        "category":"Pc",
        "configurablepone":[{"catname":"Harddisk","Pro0":"Segate 500Gb","Pro1":"Samsung 250Gb"},{"catname":"Ram","Pro0":"8Gb Ram","Pro1":"4Gb Ram","Pro2":"2Gb Ram"}],
        "configurable":"yes"
    }
]

正如你所看到的,可配置的一个JSON在每个循环中都被重复,所以我也得到了第二个产品中第一个产品的值,但我需要像下面这样分开:

下的首款产品

"configurablepone":[{"catname":"Extra","Pro0":"Extra 25g Cheese","Pro1":"Extra 75g Cheese"},{"catname":"Nuts","Pro0":"Almonds","Pro1":"Peanuts","Pro2":"Pistachios"}]

下的第二个产品

"configurablepone":[{"catname":"Harddisk","Pro0":"Segate 500Gb","Pro1":"Samsung 250Gb"},{"catname":"Ram","Pro0":"8Gb Ram","Pro1":"4Gb Ram","Pro2":"2Gb Ram"}]

我试过改变循环,但没有找到任何解决方案。请帮我解决这个问题。

我想,你在$subpro1[] = substr($subpro, 0, -1)."}";行的问题。

因此,这行的第一个调用将数据从"Veg.Pizaa"保存到$subpro1[0]中。该线路的第二次调用将数据从"Core i7 Pc"保存到$subpro1[1]中。

然后,行$subproa = "[".implode(",",$subpro1)."]";合并了所有阵列元素。

仅在array_push($totArr, $prodObj2);之后使用unset($subpro1);。以下是示例来源试试这个这个这个可能有用

$result2211 = mysql_query("select * from products where is_config = 'yes' ");
while($row2211 = mysql_fetch_assoc($result2211))
{
   $sn = $row2211['sn'];
   $allparrentselectq = mysql_query("SELECT * FROM parrentpro where parrentsn = $sn");
   while($allparrentselect = mysql_fetch_assoc($allparrentselectq))
   {
       $childarr = unserialize($allparrentselect['childsn']);
       $subpro  = '{"catname":"'.$allparrentselect['childname'].'",';
       $i = 0;
       foreach($childarr as $childarr):
           $subpro .= '"Pro'.$i++.'":"'.$childarr.'",';
       endforeach;
       $subpro1[] = substr($subpro, 0, -1)."}";
       $subproa = "[".implode(",",$subpro1)."]";
   }
   $prodObj2 = new ProductDetails();
   $prodObj2->productname = $row2211['productname'];
   $prodObj2->price = $row2211['productprice'];
   $prodObj2->discount = $row2211['discount'];
   $prodObj2->discountprice = $row2211['discountprice'];
   $prodObj2->imageURL = $row2211['productimageurl'];
   $prodObj2->category = $row2211['productcat'];
   $prodObj2->configurablepone = $subproa;
   $prodObj2->configurable = 'yes';
   array_push($totArr, $prodObj2);
   unset($subpro1);
}
while($allparrentselect = mysql_fetch_assoc($allparrentselectq))
{
    $childarr = unserialize($allparrentselect['childsn']);
    $subpro  = '{"catname":"'.$allparrentselect['childname'].'",';
    $i = 0;
    foreach($childarr as $childarr):
       $subpro .= '"Pro'.$i++.'":"'.$childarr.'",';
    endforeach;
    $subpro1[] = substr($subpro, 0, -1)."}";
    $subproa = "[".implode(",",$subpro1)."]";
    $subpro1 = array();
}

在循环时在第二秒内尝试此操作