dongzhong9055 2015-09-07 21:04
浏览 45
已采纳

打印php多维数组

I am trying to print an array.

First example.

$result2 = mysql_query("SELECT 
ps_product_attribute.id_product_attribute AS attribute_id, 
ps_advanced_attributes.pack_product_id AS base_product, 
ps_advanced_attributes.pack_product_qty AS qty_base_product, 
ps_stock_available.quantity AS base_product_on_hand, 
FLOOR(ps_stock_available.quantity / ps_advanced_attributes.pack_product_qty) AS available_to_customer 
FROM ps_product_attribute 
JOIN ps_advanced_attributes ON ps_advanced_attributes.attribute_id = ps_product_attribute.id_product_attribute 
JOIN ps_stock_available ON ps_stock_available.id_product = ps_advanced_attributes.pack_product_id 
ORDER BY ps_product_attribute.id_product_attribute ASC, ps_stock_available.quantity DESC");

echo '<pre>';   
WHILE($db_field2 = mysql_fetch_array($result2)){
print_r($db_field2);}
echo  '</pre>';

Gives this result...

Array(
    [0] => 6703
    [attribute_id] => 6703
    [1] => 382
    [base_product] => 382
    [2] => 1
    [qty_base_product] => 1
    [3] => 69
    [base_product_on_hand] => 69
    [4] => 69
    [available_to_customer] => 69
)

Only this one result

example 2...

$result2 = mysql_query("SELECT 
ps_product_attribute.id_product_attribute AS attribute_id, 
ps_advanced_attributes.pack_product_id AS base_product, 
ps_advanced_attributes.pack_product_qty AS qty_base_product, 
ps_stock_available.quantity AS base_product_on_hand, 
FLOOR(ps_stock_available.quantity / ps_advanced_attributes.pack_product_qty) AS available_to_customer 
FROM ps_product_attribute 
JOIN ps_advanced_attributes ON ps_advanced_attributes.attribute_id = ps_product_attribute.id_product_attribute 
JOIN ps_stock_available ON ps_stock_available.id_product = ps_advanced_attributes.pack_product_id 
ORDER BY ps_product_attribute.id_product_attribute ASC, ps_stock_available.quantity DESC");

echo '<pre>';   
WHILE($db_field2 = mysql_fetch_array($result2)){
print_r($db_field2);}
echo  '</pre>';

Nets this result...

Array

(

    [0] => 6703
    [attribute_id] => 6703
    [1] => 382
    [base_product] => 382
    [2] => 1
    [qty_base_product] => 1
    [3] => 69
    [base_product_on_hand] => 69
    [4] => 69
    [available_to_customer] => 69
)

Array

(

    [0] => 6703
    [attribute_id] => 6703
    [1] => 103
    [base_product] => 103
    [2] => 1
    [qty_base_product] => 1
    [3] => 4
    [base_product_on_hand] => 4
    [4] => 4
    [available_to_customer] => 4
)
Array
(

    [0] => 6703
    [attribute_id] => 6703
    [1] => 471
    [base_product] => 471
    [2] => 1
    [qty_base_product] => 1
    [3] => 0
    [base_product_on_hand] => 0
    [4] => 0
    [available_to_customer] => 0
)
Array
(

    [0] => 6704
    [attribute_id] => 6704
    [1] => 103
    [base_product] => 103
    [2] => 1
    [qty_base_product] => 1
    [3] => 4
    [base_product_on_hand] => 4
    [4] => 4
    [available_to_customer] => 4
)
Array
(

    [0] => 6704
    [attribute_id] => 6704
    [1] => 397
    [base_product] => 397
    [2] => 1
    [qty_base_product] => 1
    [3] => 0
    [base_product_on_hand] => 0
    [4] => 0
    [available_to_customer] => 0
)
Array
(

    [0] => 6704
    [attribute_id] => 6704
    [1] => 465
    [base_product] => 465
    [2] => 1
    [qty_base_product] => 1
    [3] => 0
    [base_product_on_hand] => 0
    [4] => 0
    [available_to_customer] => 0
)
Array
(

    [0] => 6705
    [attribute_id] => 6705
    [1] => 103
    [base_product] => 103
    [2] => 1
    [qty_base_product] => 1
    [3] => 4
    [base_product_on_hand] => 4
    [4] => 4
    [available_to_customer] => 4
)
Array
(

    [0] => 6705
    [attribute_id] => 6705
    [1] => 533
    [base_product] => 533
    [2] => 1
    [qty_base_product] => 1
    [3] => 2
    [base_product_on_hand] => 2
    [4] => 2
    [available_to_customer] => 2
)
Array
(

    [0] => 6705
    [attribute_id] => 6705
    [1] => 469
    [base_product] => 469
    [2] => 1
    [qty_base_product] => 1
    [3] => 0
    [base_product_on_hand] => 0
    [4] => 0
    [available_to_customer] => 0
)
Array
(

    [0] => 6706
    [attribute_id] => 6706
    [1] => 395
    [base_product] => 395
    [2] => 1
    [qty_base_product] => 1
    [3] => 41
    [base_product_on_hand] => 41
    [4] => 41
    [available_to_customer] => 41
)
Array
(

    [0] => 6706
    [attribute_id] => 6706
    [1] => 103
    [base_product] => 103
    [2] => 1
    [qty_base_product] => 1
    [3] => 4
    [base_product_on_hand] => 4
    [4] => 4
    [available_to_customer] => 4
)

Array
(

    [0] => 6706
    [attribute_id] => 6706
    [1] => 468
    [base_product] => 468
    [2] => 1
    [qty_base_product] => 1
    [3] => 0
    [base_product_on_hand] => 0
    [4] => 0
    [available_to_customer] => 0
)

As you can see, lots of results but not quite what I was expecting to see. On both examples I am getting duplicate data.

Am I doing something wrong here? I thought the first example should have printed the entire contents of the array.

  • 写回答

1条回答 默认 最新

  • doubeijian2257 2015-09-07 21:13
    关注

    You should first fetch all results to the array, and only then print it

    $result = [];
    while ($row = mysql_fetch_assoc($result2)){
        $result[] = $row;
    } 
    print_r($result);
    

    UPD: to get rid of duplicate data use mysql_fetch_assoc instead of mysql_fetch_array

    UPD2: consider using mysqli extension instead of mysql. PHP.net

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP