Solved

Help with error unexpected T_DOUBLE_ARROW

Posted on 2010-11-29
9
446 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 82

Expert Comment

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

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.

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
The viewer will learn how to count occurrences of each item in an array.

762 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

9 Experts available now in Live!

Get 1:1 Help Now