Doubt with array search

Asked

Viewed 52 times

-1

my question is the following: How do I search a key and return its value?

Ex.

$contagem = array('metodologia' => '1','facilidade' => '10')

would like to search for ease and return the value 10.

Grateful!

  • 1

    $contagem['facilidade'] accesses the value '10'. That’s what you want?

  • After the comment in the answer below, it seems to me that what you want is $contagem[$termo_pesquisado]. Please [Dit] the question and explain the problem better, because for what you asked, the answer given is perfect.

1 answer

3


You can use the name of the variable followed by the field you want to pick, for example.

$contagem['facilidade']
  • Actually this search I will do with a variable passed in the function. this stop I need to locate, it will not always be in the array. I did it with foreach and pregmatch but it became unviable, the array has a little more than 2k of record and stops it around 6k of record. I already needed to restart the apache service because of this.

  • I get the idea to search the key, if it is true it seeks the value in this way, it would be the best solution?

  • Just use if isset($contagem[$strComNomeDoCampo]) == true use the value...

  • You can use the function array_key_exists or in_array to check if the key exists in the array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.