It might be something very small that I miss but I have error of Illegal string offset 'Time' in ...
when I try to access it.
Here is the array
Array ( [message:MessageGroup] => Array ( [Header] => Array ( [ID] => none [Test] => false [Truncated] => false [Prepared] => 2022-07-06T11:27:58 ) [DataSet] => Array ( [Series] => Array ( [0] => Array ( [Obs] => Array ( [0] => Array ( [Time] => 2005 [attr] => Array ( [value] => 25.33 ) ) [1] => Array ( [Time] => 2006 [attr] => Array ( [value] => 24.74 ) ) ........... )
Here is the code how I try to access the `[‘Obs’][‘Time’]
foreach($xmlArray['message:MessageGroup']['DataSet']['Series'] as $series_key => $series) { /// foreach($series['Obs'] as $obs_kew => $obs_element) { echo $obs_element["Time"]; // Warning: Illegal string offset 'Time' in echo $obs_element["attr"]["value"]; // Warning: Illegal string offset 'attr' in } }
Any idea what is happening here?
Update: When I try $series['Obs']['Time']
instead of $obs_element["Time"]
in the second foreach, it seems to work just fine.
Advertisement
Answer
Just tried your code – and it works just fine:
<?php $xmlArray = Array ( 'message:MessageGroup' => Array ( 'Header' => Array ( 'ID' => 'none', 'Test' => false, 'Truncated' => false, 'Prepared' => '2022-07-06T11:27:58', ), 'DataSet' => Array ( 'Series' => Array ( '0' => Array ( 'Obs' => Array ( '0' => Array ( 'Time' => 2005, 'attr' => Array ( 'value' => 25.33 ) ), '1' => Array ( 'Time' => 2006, 'attr' => Array ( 'value' => 24.74 ) ) ) ) ) ) ) ); foreach($xmlArray['message:MessageGroup']['DataSet']['Series'] as $series_key => $series) { /// foreach($series['Obs'] as $obs_kew => $obs_element) { echo $obs_element["Time"]."n"; // No warnings here or anywhere else echo $obs_element["attr"]["value"]."n"; // No warnings here or anywhere else } }