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

150
Views
Dos matrices a una fragmentada

Tengo dos matrices y necesito obtener una de ellas. array_merge , array_map etc. No da el resultado correcto.

 $array1 = [1,2,3,4,5]; $array2 = [a,b,c,d,e];

Necesito $array3 = [[1,a], [2,b], [3,c]] ...

¿Cuál es la mejor manera de obtener ese resultado?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

usando array_map(null, $arr1, $arr2) puede lograr el resultado.
http://php.net/manual/en/function.array-map.php

 php > $q = array_map(null, $array1, $array2); php > print_r($q); Array ( [0] => Array ( [0] => 1 [1] => a ) [1] => Array ( [0] => 2 [1] => b ) [2] => Array ( [0] => 3 [1] => c ) [3] => Array ( [0] => 4 [1] => d ) [4] => Array ( [0] => 5 [1] => e ) )
about 4 years ago · Santiago Trujillo Report

0

El enfoque del bucle for es el siguiente:

 <?php $array1 = [1,2,3,4,5]; $array2 = ['a','b','c','d','e']; $array3 = array(); if(count($array1) == count($array2)) { for($i = 0; $i < count($array1); $i++) { $array3[] = [$array1[$i],$array2[$i]]; } } else { die("SIZE MISMATCH"); } echo '<pre>'; print_r($array3);

Y la salida es:

 Array ( [0] => Array ( [0] => 1 [1] => a ) [1] => Array ( [0] => 2 [1] => b ) [2] => Array ( [0] => 3 [1] => c ) [3] => Array ( [0] => 4 [1] => d ) [4] => Array ( [0] => 5 [1] => e ) )
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!