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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.