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

Asked

Viewed 59 times

4

how can I turn the first array of a multidimensional array into a key? for example that 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 that way

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)

2 answers

4

You can do it like 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>';

Upshot:

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 end 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"
}

4


You can use the 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"
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.