2014-09-20 109 views
0

我無法理解如何將結果拉出。我一直使用那些回聲語句來獲得「數組」,「數組」。由於從嵌套數組檢索結果

$qry = "SELECT * FROM `users` WHERE installation_id = $installation_id"; 
$res = mysqli_query($mysqli, $qry) or die('-1'.mysqli_error($mysqli)); 

$categories = array(); 
while ($row_rsCategories = mysqli_fetch_assoc($res)) { 
    $product_array = array(); 
    $product_array_query = mysqli_query($mysqli,"SELECT id, user_id, client_id, comments, stars FROM reviews WHERE user_id = '".$row_rsCategories['userId']."'"); 

    while($product_array_fetch = mysqli_fetch_array($product_array_query)) { 
     $product_array[] = array(
      "id" => $product_array_fetch['user_id'], 
      "name" => $product_array_fetch['comments'] 
     ); 
    }     

    $categories[] = array(
     'id' => $row_rsCategories['userId'], 
     'name' => $row_rsCategories['userName'], 
     'products' => $product_array, 
    ); 
} 

foreach ($categories as $category) { 
    echo $category['name']; 
    echo $category['products']; 
} 

UPDATE:

我呼籲usersName找到了一個錯誤。所以現在它將兩個用戶的名字一起打印出來。

的CATERGORY陣列是:

Array 
(
    [id] => 63 
    [name] => Paul Rothlisberger 
    [products] => Array 
     (
     ) 
) 
+0

'echo(array)'會顯示'Array'。嘗試'print_r'或'var_dump();' – 2014-09-20 17:18:16

+0

如果你嘗試'print_r($ category)'',你會得到什麼? – andy 2014-09-20 17:19:50

+0

@andy用信息更新了問題 – looloobs 2014-09-20 17:32:33

回答

0

的「陣列」被印刷,因爲這是該陣列$category['products']的字符串表示。要打印在一個逗號分隔的列表中爲每個產品的所有信息,使用:

foreach($category['products'] as $product) { 
    echo implode(",", $product); 
} 

換句話說,你必須在陣列中的每個元素遍歷到最裏面的陣列中的單個值。

更新:錯過的事實,你的產品也是陣列。更新瞭解決方案。

+0

感謝您的幫助。即使這樣,我仍然會得到「Lauren Array,ArrayPaul」這兩個用戶,第一個是唯一一個擁有產品的用戶。我需要分別打印出每個用戶。 – looloobs 2014-09-20 17:50:28

+0

更新了答案。對不起,我錯過了額外的數組,因爲問題中'Paul'的例子沒有包含另一個數組。 – andy 2014-09-20 18:02:07

+0

巨大的幫助。非常感謝。 – looloobs 2014-09-20 18:49:58