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

212
Visualizações
How to relate two arrays in PHP

I have two arrays like A & B.


A=[1,2,3,4] , B=[10,20,30,40]

I want to execute a mysql update query in a way like this.


$abc1=mysql_query("update table set corr='1' WHERE id=10");
$abc1=mysql_query("update table set corr='2' WHERE id=20");
$abc1=mysql_query("update table set corr='3' WHERE id=30");
$abc1=mysql_query("update table set corr='4' WHERE id=40");

all these query execution in one go.

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Just loop and use the index for the second array

 $as=[1,2,3,4] , $bs=[10,20,30,40]; foreach ($as as $key=>$val) { $abc1=mysqli_query("update table set corr='".$val."' WHERE id=".$bs[$key]); }

Note: you should not use mysql , use mysqli instead

Note: always escape

about 4 years ago · Santiago Trujillo Relatório

0

Using array_combine() , you can create a new array, specify one array as keys and the other as values in the new array. Then it's just a matter of looping through the resulting array and executing queries. Since you now have an array, use a foreach loop and use the keys (in this case all the values of $a ) and the values (in this case all the values of $b ) as the values you are setting.

This assumes that the number of entries in both arrays is always the same. If they are not the same size, array_combine() will return false; you can use it as verification before doing the queries.

 $a = [1, 2, 3, 4]; $b = [10, 20, 30, 40]; $result = array_combine($a, $b); foreach ($result as $k=>$v) { mysqli_query("UPDATE table SET corr='$k' WHERE id = '$v'"); }

That said, this query is vulnerable to SQL injection and you should upgrade to a newer API that supports parameterized queries with placeholders ( mysqli_* or PDO). The mysql_* API was deprecated in PHP 5.6 and completely removed in PHP7.

  • Why shouldn't I use mysql_* functions in PHP?

  • How can I prevent SQL injection in PHP?

  • Handbook for array_combine()

about 4 years ago · Santiago Trujillo Relatório

0

$a=[1,2,3,4];$b=[10,20,30,40];
$res=array_combine($a, $b );
foreach ($res as $key => $value) {
    $abc1=mysql_query("update table set corr='".$key."' WHERE id=".$value);
}
about 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