dsdqpdjpq16640651 2016-03-29 01:09
浏览 110

如何在PHP中将数据库中的项填充到二维数组中

I don't understand how to populate items into a two-dimensional array in PHP. The items are retrieved from the database and get shoved into the two-dimensional array dynamically. I am good with the database part but not the two-dimensional array part. Can someone show me how to accomplish that?

The two-dimensional array that get populated by hand has the following setup. The first array key suppose to contain strings (users: phil, sam, john, peter, jill, etc), the vaulue contains the array of strings (item#: SKU-001, SKU-002, SKU-003, etc), and the value for that array contains integer.

So three things are retrieved from the database and needed to be populated into the two-dimensional array dynamically: users, item# and the integer values.

Here is the two-dimensional array setup and populated by hand:

$products = array(
"phil"   => array("SKU-001" => 1,
                  "SKU-102" => 1,
                  "SKU-203" => 0,
                  "SKU-904" => 1,
                  "SKU-305" => 0,
                  "SKU-006" => 1),

"sam"    => array("SKU-807" => 1,
                  "SKU-608" => 1,
                  "SKU-909" => 0,
                  "SKU-111" => 0,
                  "SKU-512" => 1),

"john"   => array("SKU-013" => 1,
                  "SKU-414" => 0,
                  "SKU-115" => 1),

"peter"  => array("SKU-216" => 1,
                  "SKU-017" => 1),

"jill"   => array("SKU-618" => 1,
                  "SKU-019" => 0,
                  "SKU-120" => 1,
                  "SKU-321" => 1,
                  "SKU-222" => 1,
                  "SKU-123" => 0),

"bruce"  => array("SKU-024" => 1,
                  "SKU-925" => 1,
                  "SKU-028" => 1,
                  "SKU-129" => 1),

"tom"    => array("SKU-330" => 1)
);

How would I get the various keys and values to be populated dynamically into the two-dimensional array in PHP?

Thank you in advance!

  • 写回答

1条回答 默认 最新

  • douguanya6399 2016-03-29 01:37
    关注

    You can use foreach

    example :-

    foreach($products as as $key=>$product){
    
     echo  $key ;
     echo $product;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写