How to transform the first array of a multidimensional array into a key?

4

How can I transform the first array of a multidimensional array into a key? for example this would be my multidimensional array

array (size=5)
  0 => string 'Nome' (length=4)
  1 => string 'd_r_A' (length=5)
  2 => string 'numero' (length=6)
  3 => string 'numerodv' (length=8)
  4 => string 'codtcart' (length=8)

array (size=5)
  0 => string 'Guilherme' (length=9)
  1 => string '32132' (length=5)
  2 => string '123456' (length=6)
  3 => string '987654321' (length=8)
  4 => string '987654321' (length=8)

array (size=5)
  0 => string 'Paulo' (length=9)
  1 => string '32132' (length=5)
  2 => string '123456' (length=6)
  3 => string '987654321' (length=8)
  4 => string '987654321' (length=8)

I would like an array like this

array (size=5)
  'Nome' => string 'Guilherme' (length=9)
  'd_r_A' => string '32132' (length=5)
  'numero' => string '123456' (length=6)
  'numerodv' => string '987654321' (length=8)
  'codtcart' => string '987654321' (length=8)

array (size=5)
  'Nome' => string 'Paulo' (length=9)
  'd_r_A' => string '32132' (length=5)
  'numero' => string '123456' (length=6)
  'numerodv' => string '987654321' (length=8)
  'codtcart' => string '987654321' (length=8)
    
asked by anonymous 18.08.2016 / 21:30

2 answers

4

You can use array_map for this situation:

$array_nomes[] = ['Guilherme', '32132', '123456', '987654321', '987654321'];
$array_nomes[] = ['Paulo', '32132', '123456', '987654321', '987654321'];


$array_final = array_map(function($a) {
                                return [
                                    'Nome' => $a[0],
                                    'd_r_A' => $a[1],
                                    'numero' => $a[2],
                                    'numerodv' => $a[3],
                                    'codtcart' => $a[4]
                                ];
                           }, $array_nomes);

// Retorno:    
var_dump($array_final);

This will return:

array(2) {
  [0]=>
  array(5) {
    ["Nome"]=>
    string(9) "Guilherme"
    ["d_r_A"]=>
    string(5) "32132"
    ["numero"]=>
    string(6) "123456"
    ["numerodv"]=>
    string(9) "987654321"
    ["codtcart"]=>
    string(9) "987654321"
  }
  [1]=>
  array(5) {
    ["Nome"]=>
    string(5) "Paulo"
    ["d_r_A"]=>
    string(5) "32132"
    ["numero"]=>
    string(6) "123456"
    ["numerodv"]=>
    string(9) "987654321"
    ["codtcart"]=>
    string(9) "987654321"
  }
}
    
18.08.2016 / 22:13
4

You can do this:

$array_chaves = ['Nome', 'd_r_A', 'numero', 'numerodv', 'codtcart'];
$array_nomes[] = ['Guilherme', '32132', '123456', '987654321', '987654321'];
$array_nomes[] = ['Paulo', '32132', '123456', '987654321', '987654321'];
$array_resultado = array();

for ($i = 0; $i < count($array_nomes); $i++) {
    for ($j = 0; $j < count($array_chaves); $j++) {
        $array_resultado[$i][$array_chaves[$j]] = $array_nomes[$i][$j];
    }
}

echo '<pre>';
var_dump($array_resultado);
echo '</pre>';

Result:

array(2) {
  [0]=>
  array(5) {
    ["Nome"]=>
    string(9) "Guilherme"
    ["d_r_A"]=>
    string(5) "32132"
    ["numero"]=>
    string(6) "123456"
    ["numerodv"]=>
    string(9) "987654321"
    ["codtcart"]=>
    string(9) "987654321"
  }
  [1]=>
  array(5) {
    ["Nome"]=>
    string(5) "Paulo"
    ["d_r_A"]=>
    string(5) "32132"
    ["numero"]=>
    string(6) "123456"
    ["numerodv"]=>
    string(9) "987654321"
    ["codtcart"]=>
    string(9) "987654321"
  }
}

If you want to separate into two lists ( arrays ), just change the ending to:

echo '<pre>';
var_dump($array_resultado[0]);
echo '</pre>';

echo '<pre>';
var_dump($array_resultado[1]);
echo '</pre>';

That will return:

array(5) {
  ["Nome"]=>
  string(9) "Guilherme"
  ["d_r_A"]=>
  string(5) "32132"
  ["numero"]=>
  string(6) "123456"
  ["numerodv"]=>
  string(9) "987654321"
  ["codtcart"]=>
  string(9) "987654321"
}
array(5) {
  ["Nome"]=>
  string(5) "Paulo"
  ["d_r_A"]=>
  string(5) "32132"
  ["numero"]=>
  string(6) "123456"
  ["numerodv"]=>
  string(9) "987654321"
  ["codtcart"]=>
  string(9) "987654321"
}
    
18.08.2016 / 21:41