Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
array_search() not matching numbers with dots

i make cypher translator. I have an issue with translating encrypted characters ending with dots. I have following dictionary:

$dict = array(
     "I"=>"1",
     "L"=>"1.",
     "O"=>"0",
     "Q"=>"0.",
     "Z"=>"2",
     "?"=>"2."
);

When I use this:

function decode($cypher,$dict){

  $sepp = explode(" ",$cypher);

  foreach($sepp as $char){
    echo array_search($char,$dict);
}}

decode("1. 0. 2.",$dict);

I am getting:

IOZ

Instead of expected:

LQ?
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

By default, array_search performs the same comparison as the == operator, which will attempt to "type juggle" certain values. For instance, '1' == 1, '1.' == 1, and '1.' == '1' are all true.

That means that '1' and '1.' will always match the same element in your lookup table.

Luckily, the function takes a third argument, which the manual describes as:

If the third parameter strict is set to true then the array_search() function will search for identical elements in the haystack. This means it will also perform a strict type comparison of the needle in the haystack, and objects must be the same instance.

This makes it equivalent to the === operator instead. Note that '1' === 1, '1.' === 1, and '1.' === '1' are all false.

So you just need to add that argument:

echo array_search($char,$dict,true);
over 4 years ago · Santiago Trujillo Relatório

0

You just need to do a strict search.

See 3rd parameter of array_search.

function decode($cypher,$dict){

    $sepp = explode(" ",$cypher);

    foreach($sepp as $char){
      echo array_search($char,$dict,true);
    }
}

The reason it doesn't work without is because php thinks 1 and 1. is the same number.

over 4 years ago · Santiago Trujillo Relatório

0

This line would help you. Set third parameter to true. This causes the comparison to run in strict mode.

echo array_search($char, $dict, true);

function decode($cypher, $dict)
{
    $sepp = explode(" ",$cypher);
    foreach ($sepp as $char) {
        echo array_search($char, $dict, true);
   }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda