torvald_helmer Posted March 22, 2007 Share Posted March 22, 2007 I uses the function in_array to find out if a specified word appears in a specified array, if it is there I want to get the index of this word in the array. So I can use this index to set a value in array2, at the same index. Jeg bruker in_array for å sjekke om et bestemt ord finnes i en bestemt array, hvis det finnes vil jeg få hentet ut indeksen dette ordet har i array'en. Så vil jeg legge inn et tall i en annen array på samme index som ordet. any good tips for this? if (in_array($word, $array1)) { $i = index_of_word($array); $array2[$i] = $number_to_put_in_same_index_as_the_word_in_array1; } Link to comment https://forums.phpfreaks.com/topic/43860-get-index-of-array/ Share on other sites More sharing options...
webwiese Posted March 22, 2007 Share Posted March 22, 2007 hi, use array_search, not in_array. array_search gives you the key (index) of the search pattern if it exists. hope this will help. Link to comment https://forums.phpfreaks.com/topic/43860-get-index-of-array/#findComment-212936 Share on other sites More sharing options...
Orio Posted March 22, 2007 Share Posted March 22, 2007 You can use array_keys(). But that function returns an array because there can be more than one match. If you are sure that all of the values are unique, you can do something like this: <?php $i = array_keys($array1, $word); if (count($i) > 0) { $array2[$i[0]] = $word; } ?> I think that's what you want...? Orio. Link to comment https://forums.phpfreaks.com/topic/43860-get-index-of-array/#findComment-212937 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.