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?

Sir
  • 8,135
  • 17
  • 83
  • 146

1 Answers1

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], 
Tchoupi
  • 14,560
  • 5
  • 37
  • 71
  • Hmm it doesn't seem to loop at all i put echo 'test'; in the first foreach scope and it doesn't display.. – Sir Feb 23 '13 at 02:41
  • @Dave What's `$myObj`? Is it an instance of `coOrds`? The structure you showed is a `var_dump` of `$myObj`? – Tchoupi Feb 23 '13 at 02:48
  • `$myObj` with `print_r($myObj);` is what you see in the question :) – Sir Feb 23 '13 at 02:49
  • @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? – Tchoupi Feb 23 '13 at 02:51
  • You mean provide the class structure? – Sir Feb 23 '13 at 02:52
  • @Dave ok i edited. This might now work because it's untested but it's in the right direction. – Tchoupi Feb 23 '13 at 02:57
  • Ok i set it to public and it works kind of, i get this out put `[xy,6], [xy,7], [xy,8], ` not sure why its giving me the string rather than the value =/ – Sir Feb 23 '13 at 02:59
  • @Dave You might not call the right variable, I edited my answer to add a working example based on the information you given me. – Tchoupi Feb 23 '13 at 03:08
  • Hmm i'll investigate it see why its happening ! Thanks for the help :) – Sir Feb 23 '13 at 03:11
  • @Dave You're welcome. And don't forget to enable error reporting (http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php), you seem to be missing crucial information for efficient debugging. – Tchoupi Feb 23 '13 at 03:12