0

我的脚本中有一个PHP MySQLfetch 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"
    }
]

如您所见,每个循环都会重复可配置的pone 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"}]

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

4

3 回答 3

2

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

因此,首先调用此行将数据从“Veg.Pizaa”保存到$subpro1[0]. 此行的第二次调用将数据从“Core i7 Pc”保存到$subpro1[1].

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

于 2012-09-14T06:04:08.363 回答
0

只需unset($subpro1);array_push($totArr, $prodObj2);. 下面是一个示例来源试试这个这可能有效

$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);
}
于 2012-09-14T07:22:40.713 回答
-1
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();
}

在第二个 while 循环中尝试这个

于 2012-09-14T05:59:35.233 回答