Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda