具有动态数组命名的内部数组

时间:2014-04-22 15:54:23

标签: php mysql

如何从数据库中选择数组显示数组:

$product_array = array(
  "1" =>array('product_id'=>'100', 'product_name'=>'Apple MacBook Pro MA464LL/A 15.4" Notebook PC','product_desc'=>'The Intel Core Duo powering MacBook Pro is actually two processors built into a single chip.', 'product_price'=>'2299.99', 'product_img'=>'products/product1.jpg'),
  "2" =>array('product_id'=>'101', 'product_name'=>'Sony VAIO 11.1" Notebook PC','product_desc'=>'Weighing in at just an amazing 2.84 pounds and offering a sleek, durable carbon-fiber case in charcoal black. And with 4 to 10 hours of standard battery life, it has the stamina to power you through your most demanding applications.', 'product_price'=>'2699.99', 'product_img'=>'products/product6.jpg'),
  "102" =>array('product_id'=>'102', 'product_name'=>'Canon Digital Rebel XT 8MP Digital SLR Camera','product_desc'=>'Canon EOS Digital Rebel XT SLR adds resolution, speed, extra creative control, and enhanced comfort in the hand to one of the smallest and lightest digital cameras in its class.', 'product_price'=>'550.00', 'product_img'=>'products/product3.jpg'),
  "3" =>array('product_id'=>'103', 'product_name'=>'HTC Touch Diamond','product_desc'=>'Re-defining the perception of advanced mobile phones… the HTC Touch Diamond™ signals a giant leap forward in combining hi-tech prowess with intuitive usability and exhilarating design.', 'product_price'=>'750.00', 'product_img'=>'products/product4.jpg'),
  "5" =>array('product_id'=>'104', 'product_name'=>'Apple iMac G5 Desktop','product_desc'=>'IMAC G5/1.8 256MB 160GB SD 20IN OS10.3', 'product_price'=>'1600.00', 'product_img'=>'products/product2.jpg'),
  "4" =>array('product_id'=>'105', 'product_name'=>'Blackberry 8900','product_desc'=>'', 'product_price'=>'1150.00', 'product_img'=>'products/product5.jpg'),
  "106" =>array('product_id'=>'106', 'product_name'=>'Headphone with mic','product_desc'=>'', 'product_price'=>'148.85', 'product_img'=>'products/product8.jpg'));

这是我试过的,它只显示最后一个内部数组元素;

$getcat = mysql_query("SELECT * FROM product")or die('Error');
    $bk = array();
    $bdata = array();
    $data = array();
    while($get = mysql_fetch_array($getcat))
    {
    extract($get);
    $data[] = $get["product_id"];
    for($i=0;$i<= count($data);$i++){
    $bdata = array($i =>($bk[] = array('id'=>$product_id,'name'=>($product_name),'dew'=>$product_desc,'price'=>$product_price,'img'=>$product_img)));         }
}

感谢您的帮助

3 个答案:

答案 0 :(得分:0)

尝试这样,看看是否有帮助

$getcat = mysql_query("SELECT * FROM product")or die('Error');
    $bk = array();
    $bdata = array();
    $data = array();
    while($get = mysql_fetch_array($getcat))
    {
    extract($get);
    $data[] = $get["product_id"];
    for($i=0;$i<= count($data);$i++){
    $bdata[$product_id] = array($i =>array('id'=>$product_id,'name'=>($product_name),'dew'=>$product_desc,'price'=>$product_price,'img'=>$product_img));         }
}

答案 1 :(得分:0)

尝试此操作以在表格中显示数据:

<table border="1">
<thead>
    <tr>
        <th><b>ID</b></th>
        <th><b>Name</b></th>
        <th><b>Desc</b></th>
        <th><b>Price</b></th>
        <th><b>IMG</b></th>
    </tr>
</thead>
<tbody>
<?php
    foreach($product_array as $product){
        echo "<tr>";
        echo "<td>" . $product["product_id"] . "</td>";
        echo "<td>" . $product["product_name"] . "</td>";
        echo "<td>" . $product["product_desc"] . "</td>";
        echo "<td>" . $product["product_price"] . "</td>";
        echo "<td>" . $product["product_img"] . "</td>";
        echo "</tr>";
    }
?>
</tbody>

答案 2 :(得分:0)

我能够提出的最接近你所描述的$product_array的代码是:

$getcat = mysql_query("SELECT * FROM product")or die('Error');
$product_array = array();
while($get = mysql_fetch_array($getcat);
    $product_array[] = $get;
vardump($product_array);

索引不同,与您尝试对$bdata进行的操作相同。

您的代码中的错误是您在使用新阵列的每个周期中覆盖 $bdata。这就是为什么你只看到最后一个内部数组。你可以解决这个问题:

    $bdata[$i] = $bk[] = array('id'=>$product_id,'name'=>($product_name),'dew'=>$product_desc,'price'=>$product_price,'img'=>$product_img);

所有额外的数组和计数对我来说效率都很低。使用extract可能会使符号表变得混乱。