1

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.

S.I.
  • 3,250
  • 12
  • 48
  • 77
  • 1
    Does this answer your question? [Illegal string offset Warning PHP](https://stackoverflow.com/questions/9869150/illegal-string-offset-warning-php) – Markus Zeller Jul 06 '22 at 11:48
  • I have checked this answer before posting but I'm not sure I still understand why is that – S.I. Jul 06 '22 at 12:04
  • What version of PHP are you running? What is the output of `echo $obs_kew`? (Typo? `_key`) – tobiv Jul 06 '22 at 12:46
  • The version of the PHP is old one 5.6 and the output of `echo $obs_kew` are digits, like:`0123456789101112131415160123456701234567891011121314151617180123456789101112131415012345678910111213141516171819202122232425262728290123456789101112131415012345678910111213141516171819202122232425262728012345678910111213141501234567891011121314151617181920012345678910111213141516171819202122232425262728293031323334353637383940012345678910111213012345678910111213140123401234567891011121314151617181920012345678910111213141516171819202122232425262728290123456789101112131415` – S.I. Jul 06 '22 at 12:52
  • `echo $obs_element` is - `ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray....` – S.I. Jul 06 '22 at 12:54

1 Answers1

2

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

IVO GELOV
  • 13,496
  • 1
  • 17
  • 26
  • That's very strange. I still get the warning when trying with `$obs_element["Time"]`. But it's works when I try to get them with the `$series['Obs']['Time']`. `$series...` is from first foreach.. – S.I. Jul 06 '22 at 12:31
  • I do not get any warning ... – IVO GELOV Jul 06 '22 at 12:37
  • Works fine here https://phpize.online/sql/mysql57/undefined/php/php8/b23b69215d5c81ac892d45f68ac7a42d/ – Slava Rozhnev Jul 06 '22 at 12:53
  • @SlavaRozhnev, there is no output for PHP version 5.6. The site is using PHP version 5.6. May be this is the problem? – S.I. Jul 06 '22 at 13:02
  • @Goro, good point. – Slava Rozhnev Jul 06 '22 at 13:47
  • It runs just fine on 5.6.0 as you can see from the screenshot. – IVO GELOV Jul 06 '22 at 13:55
  • @IVOGELOV, so either the phpize.online has something against php version 5.6 or something in my code before the loops got mess, right? I can't think of anything else. – S.I. Jul 07 '22 at 06:50
  • 1
    It could be both - so to be sure, take your exact full code and try it on onlinephp.io – IVO GELOV Jul 07 '22 at 06:51
  • Thanks IVO. Accepting your answer and will continue to search what is causing the problem because after this tests I starting to believe that it is something before the loops that messing with the code. – S.I. Jul 07 '22 at 08:25