-1

I have array like below

Array
(
    [0] => stdClass Object
        (
            [id] => 67
            [contact_id] => 
            [status] => Complete
            [is_test_data] => 0
            [datesubmitted] => 2018-05-24 03:56:35
            [SessionID] => 1527148487_5b066fc7bdcd75.00827785
            [Language] => English
            [datestarted] => 2018-05-24 03:54:47
            [iLinkID] => 6528804
            [sResponseComment] => 
            [responseID] => 67
            [[question(9)]] => Yes
            [[question(24)]] => Satisfied
            [[question(25)]] => 
            [[question(26)]] => 
            [[question(27)]] => 
            [[question(28)]] => 
            [[question(29)]] => 
            [[question(31)]] => Likely
            [[question(32)]] => 
            [[question(33)]] => 26-35
            [[question(35)]] => Male
            [[question(36)]] => 
            [[question(37)]] => No
)
)

Which I am trying to derive like below

Array
(
    [0] => Array
        (
            [id] => 67
            [submitted] => 2018-05-24 03:56:35,
            [question_9]=>yes

        )
)

I simply tried array_map

$derived=array_map(function ($a){ return array('id'=>$a->id,
                                                'submitted'=>$a->datesubmitted,
                                                 'question_9'=>$a->[question(9)]//this is not a right way, what is best practice
                                                );

                               }, $survey_array);

error :Parse error: syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in
sumit
  • 15,003
  • 12
  • 69
  • 110
  • Possible duplicate of [PHP parse/syntax errors; and how to solve them?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – CBroe May 31 '18 at 11:16

1 Answers1

3

You can use curly braces to wrap special characters when accessing an object property:

$a->{'[question(9)]'}

Note: This isn't an associative array. From your initial output, $a is a stdClass object.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95