2014-09-20 110 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