Andymcc77 Posted April 6, 2009 Share Posted April 6, 2009 Hi, $find2=array_search($current_size,$prod_size_details); echo("Find2 is $find2<br />"); The above code is returning a value of 3 into $find3 which I can't understand, I expected false. The contents of the string array $prod_size_details are "2","size","4","0","M" and the value of $current_size is "L" Can anybody hazard a guess as to what may be happening here ? A previous search for "M" was made when the array contained just "2" and "size", this did return false as expected. Thanks, Andy Quote Link to comment https://forums.phpfreaks.com/topic/152842-array_search-returning-am-unexpected-value/ Share on other sites More sharing options...
jonsjava Posted April 6, 2009 Share Posted April 6, 2009 after rebuilding your code from what you said, I don't see any problem when I run it: <?php $prod_size_details = array("2","size","4","0","M"); $current_size = "L"; $find2=array_search($current_size,$prod_size_details); if ($find2 == false){ echo "Find2 is not found"; } else{ echo("Find2 is $find2<br />"); } Quote Link to comment https://forums.phpfreaks.com/topic/152842-array_search-returning-am-unexpected-value/#findComment-802635 Share on other sites More sharing options...
Maq Posted April 6, 2009 Share Posted April 6, 2009 I ran you code and it worked as I expected, this may be of interest: Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function. Quote Link to comment https://forums.phpfreaks.com/topic/152842-array_search-returning-am-unexpected-value/#findComment-802639 Share on other sites More sharing options...
Andymcc77 Posted April 6, 2009 Author Share Posted April 6, 2009 Thanks for the replies Guys. I tried testing using the === operator and no difference, I also recreated the code using the same kind of setup and I too got the right results. What I am basically doing is building up an array of clothes sizes, I check the size doesn't exist in the array first and then add in the details to the end of the array, there must be something very subtle hidden away:- All the relevant code is:- Initialisation $prod_size_details = array(); $prod_size_details[] = "2"; $prod_size_details[] = "size"; Obtain current size $current_size = trim($column); Add to array /* Check size is in master list $find=array_search($current_size,$size_array); echo("Find is $find<br />"); /* Add to array if not already there and in master list if($find != false) { for($y=0;$y < sizeof($prod_size_details);$y++) echo("prod size $prod_size_details[$y]<br />"); echo("Searching for $current_size"); $find2=array_search($current_size,$prod_size_details); echo("Find2 is $find2<br />"); if($find2 === false) { $prod_size_details[] = $size_val_array[$find]; $prod_size_details[] = 0; $prod_size_details[] = $size_array[$find]; } } Quote Link to comment https://forums.phpfreaks.com/topic/152842-array_search-returning-am-unexpected-value/#findComment-802662 Share on other sites More sharing options...
premiso Posted April 6, 2009 Share Posted April 6, 2009 if($find !== false) { Would be the correct way of doing it. Cause if size was 2, it would have returned a 0, which without the extra = is technically false. This was explained by Maq. Your code is never entering into the first if. Quote Link to comment https://forums.phpfreaks.com/topic/152842-array_search-returning-am-unexpected-value/#findComment-802663 Share on other sites More sharing options...
kenrbnsn Posted April 6, 2009 Share Posted April 6, 2009 Why don't you use in_array instead of array_search? Ken Quote Link to comment https://forums.phpfreaks.com/topic/152842-array_search-returning-am-unexpected-value/#findComment-802664 Share on other sites More sharing options...
Andymcc77 Posted April 6, 2009 Author Share Posted April 6, 2009 Hi, It is reaching the second if statement as my output confirms this, I also tried in_array and got similar results. The output is:- Processing size M Find is 3 prod size 2 prod size size Searching for M Find2 is Processing size L Find is 4 prod size 2 prod size size prod size 4 prod size 0 prod size M Searching for L Find2 is 1 Processing size XL Find is 5 prod size 2 prod size size prod size 4 prod size 0 prod size M Searching for XL Find2 is 1 Processing size 2XL Find is 6 prod size 2 prod size size prod size 4 prod size 0 prod size M Searching for 2XL Find2 is The full code is:- <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <font face="arial" size="3" color="green">Reading a delimited file using PHP by <b>G.Ajaykumar</b> </font> Email: ajaykumar_g@hotmail.com <br><br> <font color="blue" face="arial" size="4">Complete File Contents</font> <hr> <? $b= array("2","size","4","0","M"); $a="size"; $ret=array_search($a,$b); echo("Ret is $ret"); $filename = "CAT Source Attribs.csv"; echo($filename); $conn = @mysql_connect('*****', '*****', '******'); if (!$conn) { exit('<p>Unable to connect to the ' . 'database server at this time.</p>'); } if (!@mysql_select_db('fitwell_zc138a')) { exit('<p>Unable to locate the security ' . 'database at this time.</p>'); } // Retrieve Attribute Details from database // $size_array = array(); $colour_array = array(); $size_val_array = array(); $colour_val_array = array(); $prev_prod_code =""; $prev_colour =""; $prev_size =""; $prod_size_details = array(); $prod_colour_details = array(); $sql = "SELECT * from zen_products_options_values;"; $result = @mysql_query($sql); if (!$result) exit('Database Error'.mysql_error()); while ($row = mysql_fetch_array($result)) { $opt_name=$row['products_options_values_name']; $opt_val=$row['products_options_values_id']; $sql = "SELECT * from zen_products_options_values_to_products_options WHERE products_options_values_id = $opt_val"; $result2 = @mysql_query($sql); if (!$result2) exit('Database Error'.mysql_error()); $row2 = mysql_fetch_array($result2); $opt_id=$row2['products_options_id']; if ($opt_id == 2) { $size_array[]=$opt_name; $size_val_array[]=$opt_val; } if ($opt_id == 3) { $colour_array[]=$opt_name; $colour_val_array[]=$opt_val; } } echo("<br />"); foreach ($size_array as $size) echo("$size<br />"); echo("<br />"); echo("<br />"); foreach ($colour_array as $col) echo("$col<br />"); $delimiter = ","; $counter = 0; echo("Open"); $fd = @fopen ($filename, "r"); echo("Open"); if($fd) { echo("ok\n<br />");} $prod_size_details[] = "2"; $prod_size_details[] = "size"; $prod_colour_details[] = "3"; $prod_colour_details[] = "colour"; $first_time=true; $line = fgets ($fd); while(!feof($fd)) { $splitcontents = explode($delimiter, $line); $counter=0; foreach ( $splitcontents as $column ) { $counter = $counter+1; if ($counter == 1) $current_prod_code = trim($column); if ($counter == 2) $current_colour = trim($column); if ($counter == 3) $current_size = trim($column); } if (substr($current_size,-4) == "inch") { $current_size= str_replace("inch","\"",$current_size); } if (($current_prod_code != $prev_prod_code) && !$first_time) { $output_rec=$prev_prod_code; $items=0; if (sizeof($prod_size_details) > 2) { for ($x=0;$x < sizeof($prod_size_details);$x++) { $output_rec=$output_rec.","; $output_rec=$output_rec.$prod_size_details[$x]; if (((sizeof($prod_size_details) -2) % 3) == 0) $items++; } echo("Items is $items"); /* for (;$items < sizeof($size_array);$items++) { $output_rec=$output_rec.","; $output_rec=$output_rec.""; $output_rec=$output_rec.","; $output_rec=$output_rec.""; $output_rec=$output_rec.","; $output_rec=$output_rec.""; }*/ } // echo("Size table"); // echo(sizeof($prod_colour_details)); if (sizeof($prod_colour_details) > 2) { for ($x=0;$x < sizeof($prod_colour_details);$x++) { $output_rec=$output_rec.","; $output_rec=$output_rec.$prod_colour_details[$x]; } } echo("<br />Output Rec is $output_rec<br /><br />"); $output_rec=""; $prod_size_details=""; $prod_colour_details=""; $prod_size_details[] = "2"; $prod_size_details[] = "size"; $prod_colour_details[] = "3"; $prod_colour_details[] = "colour"; } echo("Processing size $current_size<br />"); if ((strcmp($current_size, "O/S") != 0) && (strcmp($current_size, $prev_size) != 0 || strcmp($prev_prod_code,$current_prod_code) != 0)){ $find=array_search($current_size,$size_array); echo("Find is $find<br />"); if($find !== false) { for($y=0;$y < sizeof($prod_size_details);$y++) echo("prod size $prod_size_details[$y]<br />"); echo("Searching for $current_size<br />"); $find2=in_array($current_size,$prod_size_details); echo("Find2 is $find2<br /><br />"); if($find2 === false) { $prod_size_details[] = $size_val_array[$find]; $prod_size_details[] = 0; $prod_size_details[] = $size_array[$find]; } } } // echo("Current Col: $current_colour Prev Col: $prev_colour Current Prod: $current_prod_code Prev Prod: $prev_prod_code"); // echo("Size of prod colours is <br />"); // echo(sizeof($prod_colour_details)); if( (strcmp($current_colour, $prev_colour) != 0) || (strcmp($prev_prod_code,$current_prod_code) != 0)) { $find=array_search($current_colour,$colour_array); if($find != false) { // echo("Colour Found"); $find2=array_search($current_colour,$prod_colour_details); // echo("Colour Exists = $find2"); if($find2 == false) { $prod_colour_details[] = $colour_val_array[$find]; $prod_colour_details[] = 0; $prod_colour_details[] = $colour_array[$find]; } } } $prev_prod_code = $current_prod_code; $prev_colour = $current_colour; $prev_size = $current_size; $first_time=false; // echo("<b>Split $counter: </b> $column<br>"); $line = fgets ($fd); } echo("END"); ?> <br><br> </body> </html> (removed DB connection stuff) Quote Link to comment https://forums.phpfreaks.com/topic/152842-array_search-returning-am-unexpected-value/#findComment-802719 Share on other sites More sharing options...
Andymcc77 Posted April 7, 2009 Author Share Posted April 7, 2009 Hi, I managed to get this to search correctly by setting parameter 3 on the in_array function to true, still not sure why that solves the problem though. Quote Link to comment https://forums.phpfreaks.com/topic/152842-array_search-returning-am-unexpected-value/#findComment-803744 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.