0

配列内でwhileループを実行できるかどうかを調べようとしています

while($row = $retail -> fetch(PDO::FETCH_ASSOC)){

$json['data'][] = array(

'id'=>$row['idretailers'],
"category"=>$row['category'],
"headline"=>$row['headline'],
'price'=> array ("full_price" => $row['price']),
'description'=>$row['description'],
"comments" => array(
while($row_c = $comments -> fetch(PDO::FETCH_ASSOC)){

 // more items  
})
);
}

コメント内に while ループがありますが、可能ですか?

ありがとう!

4

2 に答える 2

2

あなたの書き方は不可能ですが、簡単な解決策があります:

"comments" => $comments -> fetchAll(PDO::FETCH_ASSOC)
于 2012-06-20T14:20:52.033 に答える
1

JSON文字列に挿入しているので、次のようにします。

while($row = $retail -> fetch(PDO::FETCH_ASSOC)){

    $json['data'][] = array(

        "id"=>$row['idretailers'],
        "category"=>$row['category'],
        "headline"=>$row['headline'],
        "price"=> array ("full_price" => $row['price']),
        "description"=>$row['description'],
        "comments" => $comments->fetchAll()
    );
}

それ以外の場合は、コメントに対してimplodeを呼び出すことができます。

while($row = $retail -> fetch(PDO::FETCH_ASSOC)){

    $json['data'][] = array(

        "id"=>$row['idretailers'],
        "category"=>$row['category'],
        "headline"=>$row['headline'],
        "price"=> array ("full_price" => $row['price']),
        "description"=>$row['description'],
        "comments" => implode("\n", $comments->fetchAll());
    );
}
于 2012-06-20T14:49:24.157 に答える