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

153
Visualizações
Php Getting matching vaues from multidimensional arrays based on column

I'm having 2 multidimensional arrays on php, and I'm trying to get only the subarrays that have matching column value.

1st Array :

0 => {#1 ▼
    +"name": "John Doe"
    +"education": "Finance"
    +"age": "23"
    +"mark": "10"
  }
1 => {#2 ▼
    +"name": "Jane Doe"
    +"education": "History"
    +"age": "25"
    +"mark": "9"
  }
2 => {#3 ▼
    +"name": "Anne Smith"
    +"education": "Computer Science"
    +"age": "22"
    +"mark": "8"
  }

2nd Array :

0 => {#1 ▼
    +"name": "Bob Builder"
    +"country": "US"
    +"city": "Massachusetts"
  }
1 => {#2 ▼
    +"name": "Jane Doe"
    +"country": "France"
    +"city": "Paris"
  }
2 => {#3 ▼
    +"name": "Anne Smith"
    +"country": "Germany"
    +"city": "Berlin"
  }

I want to match on column name.

Desired output should be as follows :

0 => {#2 ▼
    +"name": "Jane Doe"
    +"country": "France"
    +"city": "Paris"
    +"education": "History"
    +"age": "25"
    +"mark": "9"
  }
1 => {#3 ▼
    +"name": "Anne Smith"
    +"country": "Germany"
    +"city": "Berlin"
    +"education": "Computer Science"
    +"age": "22"
    +"mark": "8"
  }

Any help on this would be appreciated.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Here is one way to do it.

First, reindex the first array by name. This will simplify matching names in the next part.

$first = array_column($first, null, 'name');

Then iterate the second array. If the person exists in the first array, merge the values from the two arrays and append that to the product. If not, don't do anything, since you're only looking for the intersection.

foreach ($second as $person) {
    if (isset($first[$person['name']])) {
        $product[] = array_merge($first[$person['name']], $person);
    }        
}

One potential problem with this is that names aren't unique, and this will break if you ever have any people with the same name. It would be much better if you had some unique identifier to use for a person rather than name.

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