Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

295
Views
PHP Buscar matriz multidimensional por valor y obtener el valor del elemento correspondiente

Estoy usando PHP y tengo una matriz multidimensional que necesito buscar para ver si existe el valor de una "clave" y, si existe, obtener el valor del "campo". Aquí está mi matriz:

 Array ( [0] => Array ( [key] => 31 [field] => CONSTRUCTN [value] => LFD_CONSTRUCTION_2 ) [1] => Array ( [key] => 32 [field] => COOLING value] => LFD_COOLING_1 ) )

Quiero poder buscar en la matriz el valor "clave" de 31. Si existe, entonces quiero poder extraer el valor de "campo" correspondiente de "CONSTRUCTN".

Intenté usar array_search(31, $myArray) pero no funciona...

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

function searchMultiArray($val, $array) { foreach ($array as $element) { if ($element['key'] == $val) { return $element['field']; } } return null; }

Y entonces:

 searchMultiArray(31, $myArray);

Debería devolver "CONSTRUCCIÓN".

about 4 years ago · Santiago Trujillo Report

0

Solución de una línea usando las funciones array_column y array_search :

 $result = array_search(31, array_column($arr, 'key', 'field')); print_r($result); // CONSTRUCTN

O con un bucle foreach simple:

 $search_key = 31; $result = ""; foreach ($arr as $item) { // $arr is your initial array if ($item['key'] == $search_key) { $result = $item['field']; break; } } print_r($result); // CONSTRUCTN
about 4 years ago · Santiago Trujillo Report

0

No lo he probado, pero creo que esto debería hacerlo.

 function searchByKey($value, $Array){ foreach ($Array as $innerArray) { if ($innerArray['key'] == $value) { return $innerArray['field']; } } }

Luego llamando a searchByKey(31, $myArray); debe devolver 'CONSTRUCCIÓN'.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!