Nuv Posted April 30, 2011 Share Posted April 30, 2011 Im getting a warning with in_array.I have used if() inside for loop to check if value has already been printed or not. If not it will print and save the value in $done[].Can someone please point me out my mistake that i am getting a warning. <?php for($i=0;$i<=$count;$i++) { $count_final = explode("_", $result[$i]); if (in_array($count_final[0],$done)) { //Line 480 echo "<tr>"; echo"<td>$count_final[0]</td>"; $templ = $count_final[0]."_left"; $tempr = $count_final[0]."_right"; $cleft = $count_middle[$templ]; $cright = $count_middle[$tempr]; echo"<td>$cright</td>"; echo"<td>$cleft</td>"; echo "</tr>"; $done[] = $count_final[0]; } } ?> Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\Users\\Desktop\yyy.php on line 480 Link to comment https://forums.phpfreaks.com/topic/235228-in_array-functionin-array-wrong-datatype-for-second-argument/ Share on other sites More sharing options...
sunfighter Posted May 1, 2011 Share Posted May 1, 2011 What part of 'Wrong datatype' don't you understand? Echo $count_final[0] and $done before the in_array to see what these two are set to. Should answer your question. Link to comment https://forums.phpfreaks.com/topic/235228-in_array-functionin-array-wrong-datatype-for-second-argument/#findComment-1209058 Share on other sites More sharing options...
wildteen88 Posted May 1, 2011 Share Posted May 1, 2011 $done should be an array. in_array() expects an array to passed to it as the second parameter. Link to comment https://forums.phpfreaks.com/topic/235228-in_array-functionin-array-wrong-datatype-for-second-argument/#findComment-1209061 Share on other sites More sharing options...
Nuv Posted May 1, 2011 Author Share Posted May 1, 2011 Yeah i sorted that out. However i got another problem.In the above code explode won't work. I wonder why. Part code from above - $count_final = explode("_", $result[$i]); $result[$i] prints Array ( [0] => 2011-05-01_right [1] => 2011-05-02_left [3] => 2011-05-02_right ) $count_final prints Array ( [0] => Array ) Array ( [0] => ) I just want the date without _right or _left thus i used explode however it wont work.Why ? Link to comment https://forums.phpfreaks.com/topic/235228-in_array-functionin-array-wrong-datatype-for-second-argument/#findComment-1209083 Share on other sites More sharing options...
wildteen88 Posted May 1, 2011 Share Posted May 1, 2011 It is is because $result[$i] returns an array, Explode requires a string as the second parameter not an array. Link to comment https://forums.phpfreaks.com/topic/235228-in_array-functionin-array-wrong-datatype-for-second-argument/#findComment-1209085 Share on other sites More sharing options...
Nuv Posted May 1, 2011 Author Share Posted May 1, 2011 It is is because $result[$i] returns an array, Explode requires a string as the second parameter not an array. if $result echo's Array ( [0] => 2011-05-01_right [1] => 2011-05-02_left [3] => 2011-05-02_right ) shouldnt $result[0] echo 2011-05-01_right ? Link to comment https://forums.phpfreaks.com/topic/235228-in_array-functionin-array-wrong-datatype-for-second-argument/#findComment-1209105 Share on other sites More sharing options...
wildteen88 Posted May 1, 2011 Share Posted May 1, 2011 I assume you're outputting the array with print_r(). $result[$i] prints Array ( [0] => 2011-05-01_right [1] => 2011-05-02_left [3] => 2011-05-02_right ) To get that output are you passing $result[$i] to print_r() or just $result? Link to comment https://forums.phpfreaks.com/topic/235228-in_array-functionin-array-wrong-datatype-for-second-argument/#findComment-1209107 Share on other sites More sharing options...
Nuv Posted May 1, 2011 Author Share Posted May 1, 2011 I am using print_r($result); outside the for loop. $result[] = array_unique($count_begin); $count = count($result); $done[] = ''; for($i=0;$i<=$count;$i++) { $string = $result[$i]; $count_final = explode("_", $result[$i]); if (!(in_array("$count_final[0]",$done))) { echo "<tr>"; echo"<td>$count_final[0]</td>"; $templ = $count_final[0]."_left"; $tempr = $count_final[0]."_right"; $cleft = $count_middle[$templ]; $cright = $count_middle[$tempr]; echo"<td>$cright</td>"; echo"<td>$cleft</td>"; echo "</tr>"; $done[] = $count_final[0]; } } echo "<pre>"; print_r($result); echo "</pre>"; Link to comment https://forums.phpfreaks.com/topic/235228-in_array-functionin-array-wrong-datatype-for-second-argument/#findComment-1209110 Share on other sites More sharing options...
wildteen88 Posted May 1, 2011 Share Posted May 1, 2011 This line $result[] = array_unique($count_begin); Will make $result a multidimensional array, as array_unqiue returns an array of unique values from the $count_begin array . Remove the [] after $result. Link to comment https://forums.phpfreaks.com/topic/235228-in_array-functionin-array-wrong-datatype-for-second-argument/#findComment-1209117 Share on other sites More sharing options...
Nuv Posted May 1, 2011 Author Share Posted May 1, 2011 This line $result[] = array_unique($count_begin); Will make $result a multidimensional array, as array_unqiue returns an array of unique values from the $count_begin array . Remove the [] after $result. Ah. Thank you for the help. Works perfectly now . Link to comment https://forums.phpfreaks.com/topic/235228-in_array-functionin-array-wrong-datatype-for-second-argument/#findComment-1209121 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.