Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-12-19
15
Medium Priority
?
1,867 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 84

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
Building an interactive eFuture classroom

Watch and learn how ATEN provided a total control system solution including seamless switching matrix switch, HDBaseT extenders, PDU, lighting control to build an interactive eFuture classroom.

 
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 400 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 111

Accepted Solution

by:
Ray Paseur earned 1600 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 111

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 34

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 111

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

Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

705 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