Link to home
Start Free TrialLog in
Avatar of Ashraf Hassanein
Ashraf Hassanein

asked on

PHP dynamically populating array getting PHP Parse error: syntax error, unexpected T_DOUBLE_ARROW

I have an object which I format it and parse it as html, meanwhile I want to populate the formatted items to an array to be sent a different page.
My code is something like:
$reply = array();
function writeList($items){
    if($items === null)
        return;
        foreach($items->children() as $i => $child){
        $y = $child->getName();
        $x = (string)$child;
        if ($x==""){$x="Header";}
        $reply(
          (string)$y => $x,
          );
        ....
       }
         writeList($child);
    }
    writeList($xml);
    var_dump($reply);
But I am getting error PHP Parse error:  syntax error, unexpected T_DOUBLE_ARROW in the line   (string)$y => $x,.
Can someone guide me how to solve it please?
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

This code doesn't make sense to me:

  
      $reply(
          (string)$y => $x,
      );

Open in new window


In fact it has the syntax as $reply were a function, but it is an array and that code I think should add an item to that array so you have to replace above lines with this one

$reply[] = (string)$y => $x;


Cheers
Avatar of Ashraf Hassanein
Ashraf Hassanein

ASKER

I found array $reply is used somewhere else in the script so I changed the name array to $output, and now I have the code like this:
$output= array();
function writeList($items){
    if($items === null)
        return;
        foreach($items->children() as $i => $child){
        $y = $child->getName();
        $x = (string)$child;
        if ($x==""){$x="Header";}
        $output[]=(string)$y => $x;
             ....
       }
         writeList($child);
    }
    writeList($xml);
    var_dump($output);
But I am still getting the same error in the same line.
$y and $x are completely separate variables and do not have any relationship that could use "=>".  If you want to combine them into one string, just use $y . $x;
Unless you are creating an associative array so you could do this

$output[(string)$y] = $x;
Now with $output[(string)$y] = $x; I get no error but with var_dump($output) I get:
array(0) { }

I want $y to be the key and $x the value in a loop
Well, this makes me suggest to use some simple but useful echo-debug:

function writeList($items){
    if($items === null)
        return;
        foreach($items->children() as $i => $child){
        $y = $child->getName();
        echo "cildname is $y<br>";
        $x = (string)$child;
        echo "child is $x<br>";
        if ($x==""){$x="Header";}
        $output[(string)$y] = $x;
        echo "x is $x and y is $y<br>";
             ....
       }
         writeList($child);
    }
No I get in the debug the following:
cildname is FirstElement
child is
x is Header and y is FirstElement
cildname is SecondElement
child is
x is Header and y is SecondElement
cildname is ThirdElement
child is 0
x is 0 and y is ThirdElement
cildname is FourthElement
child is True
x is True and y is FourthElement
cildname is FifthElement
child is ?
x is ? and y is FifithElement

array(0) { }

So what do you think?
May be that will help but the xml object is comming from a soap response:
$xml = simplexml_load_string($soapresponse);
SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes these are the expected values, I will check this code now
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried both codes but I still have the same error :-(
Maybe the best way to get a solution would be to show us the test data, and show us the output you want to get.  Then we can help bridge the gap.
greetings AshrafHassanein, , you do not seem to look for where and how you access the array $output in your  writeList( ) function, in the SCOPE of variable access (where variables can be used) you can NOT use variables from Outside of the function, Inside of the function! So in the line -
$output[(string)$y] = $x;
the function variable $output is changed (added to), , BUT this is NOT the $output you declare outside of the function! ! with -
$output = array();

you have several fixes suggested, but if you need to "Access" a variable outside of a function you can do it like this-
$GLOBALS["output"][(string)$y] = $x;
Regarding this comment, please see AntiPractice #14.  Global variables are almost always a bad idea, because they create a variable that has no encapsulation.  Consider what happens when a programmer, working in a different part of the same project, chooses a variable name for use inside a function thinking that everything is OK.  When someone sets a global variable of the same name, runtime failures will begin to occur.  It's very difficult to catch this sort of confusion in unit testing.  About the only way to avoid it is to avoid the use of global variables.  

In related matters, some of the most serious security problems in PHP arose from the idea of Register Globals, a concept which is now universally understood to be a bad idea.