Solved

Help with error unexpected T_DOUBLE_ARROW

Posted on 2010-11-29
9
447 Views
Last Modified: 2012-05-10
Hi,
Could someone help me with an error I am getting using the code and file below.
I am getting the error

unexpected T_DOUBLE_ARROW

on the line that assigns values to the array.

the file contents are:

A12345,Foo,Bar,fb@fb.com,0
B65465,Faa,Boo,bf@bf.com,0
K09878,Doo,Daa,dd@dd.com,0
CK5556,Cuu,Paa,cp@cp.com,0
$TempAuth =  array();

$file_handle = fopen("widgets.csv", "r");

while (!feof($file_handle) ) {

$line_of_text = fgetcsv($file_handle, 1024, ",");

array_push($TempAuth,VC=>$line_of_text[0],

				FN=>line_of_text[1],

				LN=>$line_of_text[2],

				EM=>$line_of_text[3],

				SP=>$line_of_text[4]);

}

Open in new window

0
Comment
Question by:nepaluz
  • 6
  • 3
9 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 34231848
try:
$TempAuth = array();
$keys=array("VC","FN","LN","EM","SP");

$file_handle = fopen("widgets.csv", "r");
while (!feof($file_handle) )
{
	$line_of_text = fgetcsv($file_handle, 1024, ",");
	$TempAuth[]=array_combine($keys,$line_of_text);
}

Open in new window

0
 
LVL 17

Author Comment

by:nepaluz
ID: 34231854
The typos in the code above are not the issue:
i.e

FN=>line_of_text[1], should be FN=>$line_of_text[1],

and I did not add two other line of code to the bottom, they are:

(I think the first of these two is throwing the error!)
$Qresult = array_search($xCode, $TempAuth);

$comma_separated = implode(",", $Qresult);

Open in new window

0
 
LVL 17

Author Comment

by:nepaluz
ID: 34231891
Thanks for that hielo.
The error is gone, however my search produces nothing. Could you guide me on that?
0
 
LVL 17

Author Closing Comment

by:nepaluz
ID: 34231929
Thanks again. I would appreciate if you couldguide me on the search too. I hope you can work out what I am trying to achieve.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 82

Expert Comment

by:hielo
ID: 34232047
at the end my last post put:
print_r($TempAuth);

You should be able to verify that $TempAuth is a multi-dimensional array. That is the reason array_search() does not find what you are after; it does not work on multi-dimensional arrays. Intead of just calling array_search($TempAuth)

you will need to do a foreach and THEN call array_search() for each value, since each "value"/item in $TempAuth is actually an array:
foreach($TempAuth as $subArray)
{
   if( FALSE !==array_search($xCode,$subArray))
   {
     $comma_separated = implode(",", $subArray);
   }
}

On another note, do you really need to save the entire file content into $TempAuth? If you are just looking for the line that contains $xCode, then you can search for $xCode while iterating over the file contents and extract/echo and/or save only the lines that contain the text that you are after.
0
 
LVL 17

Author Comment

by:nepaluz
ID: 34232214
Thanks again for the response and all.
I probably don't need to load the entire file into the array, however, my limited knowledge  of PHP has led me to believe that is the only option; you probably will be able to advise otherwise, so here goes ...

I need to check the lines in the file for $xCode and if it exists, check the last field and increment by 1, echo the email (field 3) and then save the entire array contents back to disk. It would have been a breeze in Vb.net which I usually use, however, my knowledge of PHP is extremely, what can I say ..... NON EXISTENT!

So, can you help?
0
 
LVL 17

Author Comment

by:nepaluz
ID: 34232255
Just to add, if $xCode does not exist, I need to echo an error (e.g echo "ER";), and if the count is say 5, I also need to echo another message, e.g echo "CR";
0
 
LVL 82

Expert Comment

by:hielo
ID: 34233251
>>check the last field and increment by 1
I don't quite understand if you meant to say increment the $total number of hits OR increment whatever is in "SP"

$TempAuth = array();
$keys=array("VC","FN","LN","EM","SP");

$file_handle = fopen("widgets.csv", "r");
$total=0;
while (!feof($file_handle) )
{
	$line_of_text = fgetcsv($file_handle, 1024, ",");
	if( FALSE !== array_search($xCode,$line_of_text) )
	{
		++$total;
		$temp=array_combine($keys,$line_of_text);
		
		//if you meant to increment the value in $temp["SP"]
		//uncomment the following line
		//++$temp["SP"];
		$TempAuth[]=$temp;
	}
}

if(!$total)
{
	echo 'item does not exist';
}
else
{
	echo $total.' items found';
}

Open in new window

0
 
LVL 17

Author Comment

by:nepaluz
ID: 34236517
hielo - thanks again.
after some much needed sleep, I now have got a couple of issues:

1. From the code below, when I use the line

++$line_of_text[4];

there is no increment in the number. On the other hand, the line

if ($line_of_text[4] == 0){ $line_of_text[4]=1; }

gobbles up the newline and effectively concatenates the line that matches with the next line.

2. I have been "forced" to add the else statement for onto the array_search() condition because the file saved (if I do not include it), onlycontains the matching line. I am sure I have used the same routine before WITHOUT having to include the else statement, and still have the entire array re-written to disk .... gremlins there probably.

I would appreciate your feedback.



<?php 

//$xCode = $_GET['code'];

$xCode = 'A12345';

$file = "widgets.csv";

$proverbs = file($file);

$num = count($proverbs);

echo "Count: " . $num;

$total=0;

for ($c=0; $c < $num; $c++) {

	$custom =  array_shift($proverbs);

	$line_of_text = explode(",", $custom);

	$TempAuth[]=array_combine($keys,$line_of_text);

	if( FALSE !== array_search($xCode,$line_of_text) ){

		//if ($line_of_text[4] == 0){ $line_of_text[4]=1; }

		++$line_of_text[4];

		++$total;

		$amended = implode(",",$line_of_text);

		$proverbs[] = $amended;

	} else {

		$original = implode(",",$line_of_text);

		$proverbs[] = $original;

	}

}

if(!$total) { echo 'item does not exist'; } else { 	echo $total.' items found'; }

file_put_contents($file,$proverbs);	

?>

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
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…
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).

911 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

15 Experts available now in Live!

Get 1:1 Help Now