Solved

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

Posted on 2013-12-19
15
1,748 Views
Last Modified: 2014-01-05
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?
0
Comment
Question by:Ashraf Hassanein
  • 6
  • 4
  • 3
  • +2
15 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39728632
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
0
 

Author Comment

by:Ashraf Hassanein
ID: 39728652
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.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39728658
$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;
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39728664
Unless you are creating an associative array so you could do this

$output[(string)$y] = $x;
0
 

Author Comment

by:Ashraf Hassanein
ID: 39728675
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
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39728691
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);
    }
0
 

Author Comment

by:Ashraf Hassanein
ID: 39728741
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?
0
 

Author Comment

by:Ashraf Hassanein
ID: 39728746
May be that will help but the xml object is comming from a soap response:
$xml = simplexml_load_string($soapresponse);
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 100 total points
ID: 39728875
Are those values the expected ones?

One problem is that you have more than one x == '' then you end to have more than one array key set to Header and array keys must be unique.
So I suggets to put at the very top of your script these lines

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');

Then run the script and post here the error messages you get.

Probably you should assign to x an ever new value:

function writeList($items){
    $counter = 1;
    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$counter";}
        $output[(string)$y] = $x;
        echo "x is $x and y is $y<br>";
        $counter++;
             ....
       }
         writeList($child);
    } 

Open in new window


This will grant to have unique values as array keys.
0
 

Author Comment

by:Ashraf Hassanein
ID: 39728880
Yes these are the expected values, I will check this code now
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 400 total points
ID: 39729181
Some good learning resources for people starting out in PHP are available here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

This code has a problem caused by variable scope.  The $output variable name inside the function is not the same variable as the $output outside the function.
http://www.php.net/manual/en/language.variables.scope.php

$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);

Open in new window

I have never seen anyone write a statement like this:

 $output[(string)$y] = $x;

Here is a suggestion that is untested but might help bring about good results.
function writeList($items)
{
    // IF THE INPUT IS NULL
    if($items === null) return FALSE;
    
    // ITERATE OVER THE ITEMS CALLING THE children() METHOD
    foreach($items->children() as $i => $child)
    {
        // CAST THE RESULT OF THE getName() METHOD TO A STRING
        $y = (string)$child->getName();
        
        // CAST THE ENTIRE child OBJECT TO A STRING
        $x = (string)$child;
        
        // IF THE child OBJECT STRING IS A NULL STRING
        if ($x == "")
        {
            $x = "Header";
        }
        
        // COLLECT THE INFORMATION HERE
        $output[$y] => $x;
    }
    
    // RETURN THE $output ARRAY FROM THE FUNCTION TO THE CALLING SCOPE
    return $output
}

// CALL THE FUNCTION AND USE THE RETURNED ARRAY
$thing = writeList($xml);
var_dump($thing);

Open in new window

Best to all, over and out, ~Ray
0
 

Author Comment

by:Ashraf Hassanein
ID: 39729331
I tried both codes but I still have the same error :-(
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39729404
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.
0
 
LVL 33

Expert Comment

by:Slick812
ID: 39732663
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;
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39732736
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.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question