Skip to content
Advertisement

Can’t find an reason for illegal string offset when trying to access data from array

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

enter image description here

enter image description here

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement