-2

I'm new to PHP and I'm trying to insert an array inside another array.

$int = array();
array_push($int, array('begin' => 0, 'end' => 10));
array_push($int, array('begin' => 10, 'end' => 20))
array_push($int, array('begin' => 30, 'end' => 30))

And I'm trying to read it:

foreach ($int as $sint) {
    echo $int->begin;
    echo $int->end;
}

But I receive:

Trying to get property of non-object in

What am I doing wrong?

2
  • This is an array and you're trying to access as if it was an object. Instead of using ->, try using $int['begin'] Commented Dec 18, 2017 at 16:02
  • 1
    Possible duplicate of PHP - Accessing Multidimensional Array Values Commented Dec 18, 2017 at 16:03

1 Answer 1

3

You have to use [] to access to items in array. -> is for accessing the object items.

In foreach loop single items are in $sint, not $int.

foreach ($int as $sint) {
   echo $sint['begin'];
   echo $sint['end'];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.