Solved

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

Posted on 2013-12-19
15
1,703 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 30

Expert Comment

by:Marco Gasi
Comment Utility
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
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
$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
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
Unless you are creating an associative array so you could do this

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

Author Comment

by:Ashraf Hassanein
Comment Utility
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 30

Expert Comment

by:Marco Gasi
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

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

Assisted Solution

by:Marco Gasi
Marco Gasi earned 100 total points
Comment Utility
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
Comment Utility
Yes these are the expected values, I will check this code now
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 400 total points
Comment Utility
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
Comment Utility
I tried both codes but I still have the same error :-(
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now