0

I have a php variable that I got from a _POST. var_dump shows this:

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" } }

I loop through the array but can't get the properties to print:

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

age and amount are blank:

age=, amount=
2
  • Currently on mobile but have you tried $incomeSched[$i]['age']? Commented Jun 3, 2018 at 0:10
  • Yes, that worked. Thank you! Commented Jun 3, 2018 at 0:44

2 Answers 2

0

There's a difference between associative arrays and objects.

$incomeSched[$i]->age;

is what you'd do to access the property of an object. For an associative array you'd want

$incomeSched[$i]["age"]

You can cast an array as an object if need be:

$obj = (object)$incomeSched;

learn more here:

PHP - associative array as an object

Sign up to request clarification or add additional context in comments.

Comments

0

As far as I remember ->age is object syntax. You need array syntax which would be ['age'].

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

1 Comment

Thank you. You are right but so was @Michael Beeson and he gave a little more info that will help others searching for this so I gave him the credit for the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.