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; } Quote Link to comment 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. Quote Link to comment 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. 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.