如何从以下数组中检索p_name和total_qty的值?

时间:2017-09-10 06:37:01

标签: php arrays

string(1) "5" string(5) "shop2" array(7) { 
    [0]=> array(2) { ["p_name"]=> string(9) "Coca Cola" ["total_qty"]=> string(1) "5" } 
    [1]=> array(2) { ["p_name"]=> string(13) "Fried Chicken" ["total_qty"]=> string(1) "8" } 
    [2]=> array(2) { ["p_name"]=> string(10) "Fried Rice" ["total_qty"]=> string(1) "3" } 
    [3]=> array(2) { ["p_name"]=> string(16) "Fried Vermicelli" ["total_qty"]=> string(1) "5" } 
    [4]=> array(2) { ["p_name"]=> string(13) "Friedn Noodle" ["total_qty"]=> string(1) "5" } 
    [5]=> array(2) { ["p_name"]=> string(5) "Sushi" ["total_qty"]=> string(1) "5" } 
    [6]=> array(2) { ["p_name"]=> string(12) "Test Product" ["total_qty"]=> string(1) "2" } }
  

块引用   上面的数组名称是$ itemm..i,称为$ itemm ['p_name'],但我不能。

2 个答案:

答案 0 :(得分:0)

$ itemm [p_name]中的错误 您可以更改为$ itemm ['p_name']

答案 1 :(得分:0)

使用您发布的输入数组,您可以将子数组值打印到表格行中:

代码:(Demo

$itemm=["5",
        "shop2",
        [
            ["p_name"=>"Coca Cola","total_qty"=>"5"],
            ["p_name"=>"Fried Chicken","total_qty"=>"8"],
            ["p_name"=>"Fried Rice","total_qty"=>"3"],
            ["p_name"=>"Fried Vermicelli","total_qty"=>"5"],
            ["p_name"=>"Friedn Noodle","total_qty"=>"5"],
            ["p_name"=>"Sushi","total_qty"=>"5"],
            ["p_name"=>"Test Product","total_qty"=>"2"]
        ]
    ];
foreach($itemm[2] as $rows){
    echo "<tr><td>{$rows['p_name']}</td><td>{$rows['p_name']}</td></tr>\n";
}

输出:

<tr><td>Coca Cola</td><td>Coca Cola</td></tr>
<tr><td>Fried Chicken</td><td>Fried Chicken</td></tr>
<tr><td>Fried Rice</td><td>Fried Rice</td></tr>
<tr><td>Fried Vermicelli</td><td>Fried Vermicelli</td></tr>
<tr><td>Friedn Noodle</td><td>Friedn Noodle</td></tr>
<tr><td>Sushi</td><td>Sushi</td></tr>
<tr><td>Test Product</td><td>Test Product</td></tr>