2012-09-14 158 views
0

我有一個PHPMySQL在我的腳本如下所示while循環獲取。我得到的結果是類似下面(JSON):PHP while循環錯誤

[ 
    { 
     "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" 
    } 
] 

正如你可以看到configurablepone得到重複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"}] 

我試圖改變的循環,但我還沒有找到任何解決方案。請幫我解決這個問題。

+1

的foreach($ childarr爲$ childarr):< - 這樣不是會使問題?他們似乎和我有同樣的名字。 – Shadow

+0

@shadow我已經嘗試更改每個名稱,但它導致相同的問題 –

+2

歡迎來到堆棧溢出!請不要使用'mysql_ *'函數來編寫新的代碼。他們不再維護,社區已經開始[棄用程序](http://goo.gl/KJveJ)。請參閱* [紅盒子](http://goo.gl/GPmFd)*?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定哪些,[這篇文章](http://goo.gl/3gqF9)會幫助你。如果你選擇PDO,[這裏是很好的教程](http://goo.gl/vFWnC)。 –

回答

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); 
} 
-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循環

+0

不能同樣的問題 –

2

我認爲,在線路$subpro1[] = substr($subpro, 0, -1)."}";您的問題。

因此,該行的第一個呼叫將「Veg.Pizaa」的數據保存到$subpro1[0]。 該行的第二個調用將數據從「Core i7 Pc」保存到$subpro1[1]

然後,行$subproa = "[".implode(",",$subpro1)."]";合併所有數組元素。

+0

你能解釋一下嗎?更多Sir –

+1

'$ subpro'是當前項目的子產品列表。在'while'循環中,我們得到比薩的子過程,並將它保存在數組'$ subpro1'的第一個元素中($ subpro1 [0]包含值:''Pro0「:」額外25g奶酪「,」Pro1「:」Extra 75g Cheese然後我們得到處理器的副產品,並將它保存在數組'subpro1'的第二個元素中($ subpro1 [1]包含'「Pro0」:「Segate 500Gb」,「Pro1」:「Samsung 250Gb」'。函數'implode'將兩個元素都合併到第二個產品的一個字符串中。因此,在使用新數據填充之前,數組'$ subpro1'必須爲空。通過'$ subpro1 = array()'或'unset($ subpro1)'。 – doktorgradus

+0

如何可以在動物中可以向我展示一個例子先生 –