0

I have an object which holds a 2 dimensional array but I can't seem to get the output working.

I look the data like this:

foreach($myObj as $key){ foreach($myObj[$key] as $key2){ echo '['.$key.','.$key2.'], '; } } 

But i get no output just a blank page. I also have no error's in my error log.

My object is structured like this:

coOrds Object ( [xy:coOrds:private] => Array ( [10] => Array //value 10 would be $key ( [10] => //10 here would be $key2 [11] => [12] => ) [11] => Array ( [10] => [11] => [12] => ) ) } 

What am i doing wrong for my loops?

1 Answer 1

1

This should work:

foreach($myObj as $key => $array){ foreach($array as $key2 => $array2){ echo '['.$key.','.$key2.'], '; } } 

Edit

Since xy is private, you should either:

  • make it public
  • create a simple getter public function getXY() { return $this->xy; }

Then:

foreach($myObj->getXY() as $key => $array){ foreach($array as $key2 => $array2){ echo '['.$key.','.$key2.'], '; } } 

Edit 2

A working sample:

class coOrds { public $xy = array(10 => array(1, 2, 3), 11 => array(4,5,6)); } $myObj = new coOrds(); foreach($myObj->xy as $key => $array){ foreach($array as $key2 => $array2){ echo '['.$key.','.$key2.'], '; } } 

Output:

[10,0], [10,1], [10,2], [11,0], [11,1], [11,2], 
Sign up to request clarification or add additional context in comments.

10 Comments

Hmm it doesn't seem to loop at all i put echo 'test'; in the first foreach scope and it doesn't display..
@Dave What's $myObj? Is it an instance of coOrds? The structure you showed is a var_dump of $myObj?
$myObj with print_r($myObj); is what you see in the question :)
@Dave Then xy is a private property, in that case you would not be able to access it? Can you include the source code of your object?
You mean provide the class structure?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.