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

108
Views
Create a multidimensional array based on number of values

i have an array that i get from a form with alot of fields, i need to order those fields under an array and that array under the ''main'' array. Ill post what i have, and what i need to get. WHAT I HAVE:

Array
(
    [perfil_area1] => Array
    (
        [0] => a2
        [1] => a3
    )

    [perfil_years] => Array
    (
        [0] => 2
        [1] => 4
    )

    [perfil_function] => Array
    (
        [0] => f1
        [1] => f4
    )

    [perfil_obs] => Array
    (
        [0] => teste1
        [1] => teste2
    )

    [perfil_company] => Array
    (
        [0] => emp1
        [1] => emp2
    )
)

This is what i need to be so i can turn it into a query: What i need

Array
(
    [0] => 
    (
        [perfil_area1] => a2
        [perfil_years] =>  2
        [perfil_function] =>  f1
        [perfil_obs] =>  teste1
        [perfil_company] =>  emp1
    )
    [1] =>
    (
        [perfil_area1] => emp2
        [perfil_years] =>  2
        [perfil_function] =>  f4
        [perfil_obs] =>  4
        [perfil_company] =>  a3
    )
)

i have tried with 2 foreach but i didnt manage to get it done. I have read Create a multidimensional array in a loop , how to create multidimensional array using a foreach loop in php? , create multidimensional array using a foreach loop , Converting an array from one to multi-dimensional based on parent ID values and some more but i still cant do it. Any tips on how to create it?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You just need to loop over the input array and build the new array

$new = [];
foreach ($in as $key=>$arr) {
    $new[0][$key] = $arr[0];
    $new[1][$key] = $arr[1];
}
over 4 years ago · Santiago Trujillo Report

0

<?php

$result = [];

for ($i=0; $i<count(reset($array)); $i++) {
    $result[] = array_combine(array_keys($array), array_column($array, $i));
}

Please read the manual for more details about array_combine(), array_keys() and array_column().

over 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!