Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1895
  • Last Modified:

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?
0
Ashraf Hassanein
Asked:
Ashraf Hassanein
  • 6
  • 4
  • 3
  • +2
2 Solutions
 
Marco GasiFreelancerCommented:
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
 
Ashraf HassaneinAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
$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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Marco GasiFreelancerCommented:
Unless you are creating an associative array so you could do this

$output[(string)$y] = $x;
0
 
Ashraf HassaneinAuthor Commented:
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
 
Marco GasiFreelancerCommented:
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
 
Ashraf HassaneinAuthor Commented:
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
 
Ashraf HassaneinAuthor Commented:
May be that will help but the xml object is comming from a soap response:
$xml = simplexml_load_string($soapresponse);
0
 
Marco GasiFreelancerCommented:
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
 
Ashraf HassaneinAuthor Commented:
Yes these are the expected values, I will check this code now
0
 
Ray PaseurCommented:
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
 
Ashraf HassaneinAuthor Commented:
I tried both codes but I still have the same error :-(
0
 
Ray PaseurCommented:
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
 
Slick812Commented:
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
 
Ray PaseurCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now