drfqfuhej48511519 2015-10-07 16:39
浏览 17

如何在数据库值中打印php中的2d数组

I need to print an 2d array like this from values fetched from database having table 'product' whose structure is like this

name   product_name   rating 
rahul  fifa 15         3
rahul  gta 5           4
bidur  GOD OF WAR     3.5
bidur  gta 5          4

I wan't output like below using php/mysql

$books = array(

"rahul" => array("fifa 15" => 3,"gta 5" => 4),

"bidur" => array("GOD OF WAR" => 3.5,"gta 5" => 4),

"sushil" => array("HEAVY RAIN" => 4),

"vivek" => array("FIFA 15" => 3,"gta 5" => 4),

"anup" => array("Read Dead Redemption" => 4.5),

"nikhil" => array("FAR CRY 4" => 4.5,"HEAVY RAIN" => 3),

"akhi" => array("WWE RAW" => 3.5)

);

i have tried this i am using php/mysql I wan't to get table data into a 2d array and i have tried like this

$books=array();

$sql="select distinct(email) from product_order";
$result=mysqli_query($con,$sql) or die($con->error);

while($row=mysqli_fetch_array($result))
{

    $email=$row['email'];
    $query="select product_name,rating from product_order where email='$email'";
    $result=mysqli_query($con,$query) or die($con->error);


    while($r=mysqli_fetch_array($result))
    {
        $books["$email"][]=$r["product_name"];
        $books["$email"][]=$r["rating"];
    }

}


if($result) echo 'success';
else 'failure';

print_r($books);

but it is not working

  • 写回答

1条回答 默认 最新

  • doudui6756 2015-10-07 16:56
    关注

    So if you want product_name field be a key and rating field be a value:

    while($r=mysqli_fetch_array($result))
    {
        $books["$email"][$r["product_name"]]=$r["rating"];
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题