Jump to content

Get index of array?


torvald_helmer

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.