Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组中对象的php访问属性_Php_Arrays - Fatal编程技术网

数组中对象的php访问属性

数组中对象的php访问属性,php,arrays,Php,Arrays,我有一个php变量,是从一篇文章中得到的。var_dump显示了这一点: array(9) { [0]=> array(2) { ["age"]=> string(2) "62" ["amount"]=> string(5) "10878" } [1]=> array(2) { ["age"]=> string(2) "63" ["amount"]=> string(5) "10878" } [2]=> array(2) { ["age"]=> s

我有一个php变量,是从一篇文章中得到的。var_dump显示了这一点:

array(9) { [0]=> array(2) { ["age"]=> string(2) "62"
["amount"]=> string(5) "10878" } [1]=> array(2) { ["age"]=> string(2) "63"
["amount"]=> string(5) "10878" } [2]=> array(2) { ["age"]=> string(2) "64"
["amount"]=> string(5) "10878" } [3]=> array(2) { ["age"]=> string(2) "65"
["amount"]=> string(5) "10878" } [4]=> array(2) { ["age"]=> string(2) "66"
["amount"]=> string(5) "10878" } [5]=> array(2) { ["age"]=> string(2) "67"
["amount"]=> string(5) "28416" } [6]=> array(2) { ["age"]=> string(2) "68" 
["amount"]=> string(5) "28416" } [7]=> array(2) { ["age"]=> string(2) "69" 
["amount"]=> string(5) "28416" } [8]=> array(2) { ["age"]=> string(2) "70" 
["amount"]=> string(5) "28416" } }
我在数组中循环,但无法获取要打印的属性:

for ($i=0; $i<count($incomeSched); $i++) {
    $age = $incomeSched[$i]->age;
    $amt = $incomeSched[$i]->amount;
    echo "age=$age, amount=$amt<br>";
}

就我所记得的
->age
是对象语法。您需要的数组语法应该是
['age']

for ($i=0; $i<count($incomeSched); $i++) {
    $age = $incomeSched[$i]['age'];
    $amt = $incomeSched[$i]['amount'];
    echo "age=$age, amount=$amt<br>";
}

对于($i=0;$i关联数组和对象之间存在差异

$incomeSched[$i]->age;
是访问对象属性时要执行的操作。对于关联数组,您需要

$incomeSched[$i]["age"]
如果需要,可以将数组强制转换为对象:

$obj = (object)$incomeSched;
在此处了解更多信息:


目前正在使用手机,但您是否尝试过$incomeSched[$i]['age']?是的,这很有效。谢谢!谢谢。您是对的,但@Michael Beeson也是,他提供了更多信息,帮助其他人搜索此信息,因此我将答案归功于他。
$obj = (object)$incomeSched;