weazle82 Posted October 16, 2007 Share Posted October 16, 2007 Hey....i have got a problem with the array_search. It might be quite simple...but i doesnt work at all. I am trying to get the position of a String inside an array. This is how one of my array looks like ($array) Array ([0] => 1533 [1] => 4711 [2] => 3311 [3] => 1234) snippet 1 (does not work) $search = 4711; $position = array_search($search , $array); echo $position"; Result = snippet 2 (works fine) // $search = 4711; $position = array_search(4711 , $array); echo $position; Result = 1 Why can't I use my $search inside the array_search?! Does anyone know about this "feature" ?! Regards from GER, Chris Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted October 16, 2007 Share Posted October 16, 2007 Try changing your search variable to this: $search = '4711'; It may be reading the number as a string inside the array, so making it a string in the variable may work. Quote Link to comment Share on other sites More sharing options...
weazle82 Posted October 16, 2007 Author Share Posted October 16, 2007 Try changing your search variable to this: $search = '4711'; It may be reading the number as a string inside the array, so making it a string in the variable may work. Hi...i have already tried that. Its some kind of strange. This part does not work: <?php $result = mysql_query("SELECT * FROM preise_neu;") or die("Fehler: <br>". mysql_error()); while ($newarticles = mysql_fetch_assoc($result)) { $update_datensatz = array($newarticles['artnr'], $newarticles['preisa'], $newarticles['preisb'], $newarticles['preisc']); $artikel = mysql_query("SELECT * FROM artikel;") or die("Fehler: <br>". mysql_error()); while ($wert = mysql_fetch_assoc($artikel)) { $check = trim($wert['artnr']); $parts = explode("\n", $check); $search = $update_datensatz[0]; print_r ($teile); $position = array_search($search, $parts); echo "Suchwert befindet sich an Stelle: " . $position . "<br><br><br>"; } }?> But this part works fine <?php $array = explode("\n", "34 34 346 97 9789 78 678 3"); $search = $array[4]; print_r ($array); echo "<br><br>" . "Count elements: " . count ($array) . "<br>"; $position = array_search($search, $array); echo "Element on position: " . $position . "<br><br><br>"; ?> Hope I was able to make it a little comprehensible .... :-P Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 16, 2007 Share Posted October 16, 2007 Your first snippet worked fine for me: <?php $arr = Array (1533, 4711, 3311, 1234); $search = 4711; $position = array_search($search , $arr); echo $position."\n"; ?> Ken Quote Link to comment Share on other sites More sharing options...
weazle82 Posted October 16, 2007 Author Share Posted October 16, 2007 OMG.....can't belive it. My "explode" was not clean... The record delimiter inside my database fields was "\r\n" and not "\n”. Shame on me Thanks for help guys! Quote Link to comment 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.