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 Link to comment https://forums.phpfreaks.com/topic/73536-solved-array_search-problem/ 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. Link to comment https://forums.phpfreaks.com/topic/73536-solved-array_search-problem/#findComment-371011 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 Link to comment https://forums.phpfreaks.com/topic/73536-solved-array_search-problem/#findComment-371034 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 Link to comment https://forums.phpfreaks.com/topic/73536-solved-array_search-problem/#findComment-371039 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! Link to comment https://forums.phpfreaks.com/topic/73536-solved-array_search-problem/#findComment-371100 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.