使用php显示数组中的值

时间:2016-01-19 11:57:10

标签: php mysql arrays

如何在下面的例子中使用php foreach:

的var_dump($ dasboardStat);

array(4) { 
    [0]=> object(stdClass)#4 (2) { 
        ["expenseType"]=> string(13) "Communication" 
        ["totalprice"]=> string(2) "99" 
    } 
    [ 1 ]=> object(stdClass)#5 (2) { 
        ["expenseType"]=> string(14) "Sales/Purchase" 
        ["totalprice"]=> string(4) "1900" 
    } 
    [ 2 ]=> object(stdClass)#6 (2) { 
        ["expenseType"]=> string(7) "Vehicle" 
        ["totalprice"]=> string(3) "210" 
    } 
    [3]=> object(stdClass)#7 (2) { 
        ["expenseType"]=> NULL 
        ["totalprice"]=> string(4) "2209" 
    } 
}

数组是mysql查询的结果,下面是sql查询和结果截图。 expenseType将超过3种类型,在下面的情况下只有三种类型已输入数据库。

the sql query

the result array

2 个答案:

答案 0 :(得分:0)

希望这有帮助。

foreach($dasboardStat as $row){
    $expenseType = $row->expenseType;
    $totalprice = $row->totalprice;

    // Do what you want ....
}

答案 1 :(得分:0)

foreach($array as $object){
    foreach($object as $key){
        echo $key . " => " . $object->$key;
    }
}
// Will output:
// "expenseType => Communication
// totalprice => 99"
// and so on...