0

I have an object, within an array, that is itself within an array - like this:

Array ( 
    [0] => Array 
        ( 
             [year] => 2010 
             [0] => stdClass Object 
                    ( 
                           [ticker] => AAA
                           [exchange] => LSE
                           [cur_value] => 100 
                     )
             [1] => stdClass Object 
                    ( 
                           [ticker] => BBB
                           [exchange] => LSE
                           [cur_value] => 200 
                     )
          )
    [1] => Array 
          ( 
              [year] => 2015 
              [0] => stdClass Object 
                        ( 
                            [ticker] => AAA
                            [exchange] => LSE
                            [cur_value] => 100 
                         )
                 [1] => stdClass Object 
                        ( 
                            [ticker] => BBB
                            [exchange] => LSE
                            [cur_value] => 200 
                         )
              )
           )

I want to be able to loop through the first level array, and then create a table with the data it contains. The table will use the 'year' as its name, and the the stdClass Objects will be the data that goes into the table.

The problem I am having is that that the ['year'] interferes with the first row of the table and I cannot seem to skip it - e.g. by putting a counter in and skipping the first foreach iteration or by using is_object.

The error I am getting (and I understand why I get it) is 'You are trying to get property of non-object'.

I have looked at other SO questions but they I haven't been able to find this exact problem e.g this one - Notice: Trying to get property of non-object error

Can anyone help me skip that 'year'? The PHP code I have is like this:

foreach($portfolios as $portfolio){
  echo $portfolio['year'];
  echo '<table>';
  echo '<tr>';
     echo '<th>Ticker</th>';
     echo '<th>Exchange</th>';
     echo '<th>Value</th>';
  echo '</tr>';
  foreach($portfolio as $folio){
     echo '<tr>';
     echo '<td>'.$folio->ticker.'</td>';
     echo '<td>'.$folio->exchange.'</td>';
     echo '<td>'.$folio->cur_value.'</td>';
     echo '</tr>';
   }
  echo '</table>';

}
HG123
  • 23
  • 5
  • `echo $portfolio['year']; unset($portfolio['year']);` – splash58 Aug 13 '18 at 22:07
  • Thanks - this worked! Other solutions made sense as well, and as Stony says I am use there are loads of ways to solve this, but went with simplest. Thanks – HG123 Aug 13 '18 at 22:16
  • I would suggest cleaning up that data though, having a separate key for all the data won't hurt, having something like `array("year" => "YOUR_YEAR", "data" => "ARRAY_OF_DATA")` would be nice. – 0yeoj Aug 13 '18 at 22:33

1 Answers1

0

You could use array_splice() to remove the first element of the array so something like this:

$splicedArray = array_splice($portfolio, 0, 1);

foreach(splicedArray as $folio) {
    echo '<tr>';
    echo '<td>'.$folio->ticker.'</td>';
    echo '<td>'.$folio->exchange.'</td>';
    echo '<td>'.$folio->cur_value.'</td>';
    echo '</tr>';
}

You could take a look at array_shift() or you use the key and skip that iteration in your loop. There are a lot of possibilities to solve that problem.

Or very simple use unset.

René Höhle
  • 26,716
  • 22
  • 73
  • 82