Link to home
Start Free TrialLog in
Avatar of nepaluz
nepaluzFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Help with error unexpected T_DOUBLE_ARROW

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

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nepaluz

ASKER

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

Avatar of nepaluz

ASKER

Thanks for that hielo.
The error is gone, however my search produces nothing. Could you guide me on that?
Avatar of nepaluz

ASKER

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.
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.
Avatar of nepaluz

ASKER

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?
Avatar of nepaluz

ASKER

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

Avatar of nepaluz

ASKER

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