Solved

Help with error unexpected T_DOUBLE_ARROW

Posted on 2010-11-29
9
450 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress and Wufoo 1 36
EditableGrid how to fetch rows from MySql in php 14 44
Make login page safer 3 30
How to remove a section of the main menu from Magento 1.9.3 3 23
Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
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…

713 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