-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?

FirstOne
  • 6,033
  • 7
  • 26
  • 45
J.Doe
  • 1
  • 1
  • This is an array and you're trying to access as if it was an object. Instead of using `->`, try using `$int['begin']` – Phiter Dec 18 '17 at 16:02
  • 1
    Possible duplicate of [PHP - Accessing Multidimensional Array Values](https://stackoverflow.com/questions/17139453/php-accessing-multidimensional-array-values) – FirstOne Dec 18 '17 at 16:03

1 Answers1

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'];
}
pavel
  • 26,538
  • 10
  • 45
  • 61