Reputation: 371
I'm looking for a solution to create an array of associative arrays from an array of indexed array where the first row should be used as the keys for all subsequent rows.
What I have is a csv/xls file that has header values on the first row and data in all remaining rows.
Row1: header Row2,3,4,5: data
the array looks:
[
[
"country",
"state",
"city",
"name",
"address",
"gender",
"status"
],
[
"Argentina",
"Corrientes",
"Corrientes",
"Jorge",
"Avenida Avellaneda 12",
"Masculino",
"Activo"
],
[
"Argentina",
"Chaco",
"Resistencia",
"Mariano",
"Avenida Peron 12",
"Masculino",
"Activo"
]
]
The result i need to get at the end is:
[
[
'country' => 'Argentina',
'state' => 'Corrientes',
'city' => 'Corrientes',
'name' => 'Jorge',
'address' => 'Avenida Avellaneda 12',
'gender' => 'Masculino',
'status' => 'Activo',
],
[
'country' => 'Argentina',
'state' => 'Chaco',
'city' => 'Resistencia',
'name' => 'Mariano',
'address' => 'Avenida Peron 12',
'gender' => 'Masculino',
'status' => 'Activo',
]
]
Upvotes: 0
Views: 92
Reputation: 47894
array_walk()
is conveniently equipped to pass in an additional data set to be accessed by each iteration. Use the header data to modify each row after stripping the first row from the original input array.
Code: (Demo)
array_walk(
$array,
fn(&$row, $_, $header) => $row = array_combine($header, $row),
array_shift($array)
);
var_export($array);
array_reduce()
can generate a new array with the header data distributed across all remaining rows. (Demo)
var_export(
array_reduce(
$array,
function ($result, $row) {
static $header;
if (!$header) {
$header = $row;
} else {
$result[] = array_combine($header, $row);
}
return $result;
},
[]
)
);
Upvotes: 0
Reputation: 2792
create a multidimensional array from an flat array
You already have a multidimensional array, because you got arrays in an array.
What you can do in this specific case is to use array_splice() in combination with array_combine().
Try this:
$oldArray = array(
array( "country", "state", "city", "name" ),
array( "Argentina", "Corrientes", "Corrientes", "Jorge" ),
array( "Argentina", "Chaco", "Resistencia", "Mariano" )
);
$newArray = array_splice( $oldArray, 1 );
foreach( $newArray as $index => $array ) {
$newArray[$index] = array_combine( $oldArray[0], $array );
}
echo "<pre>";
var_dump( $newArray );
OUTPUT:
array(2) {
[0]=>
array(4) {
["country"]=>
string(9) "Argentina"
["state"]=>
string(10) "Corrientes"
["city"]=>
string(10) "Corrientes"
["name"]=>
string(5) "Jorge"
}
[1]=>
array(4) {
["country"]=>
string(9) "Argentina"
["state"]=>
string(5) "Chaco"
["city"]=>
string(11) "Resistencia"
["name"]=>
string(7) "Mariano"
}
}
Upvotes: 1
Reputation: 255
$array = $your_flat_array;
for ($i = 1; $i < count($array); $i++) {
$new_array[$i-1] = [];
foreach ($array[$i] as $key => $value) {
$new_array[$i-1][$array[0][$key]] = $value;
}
}
print_r($new_array);
Upvotes: 1
Reputation: 640
All you have to do is remove the first item (header row) from your array:
array_splice($yourArray, 0, 1);
Upvotes: 0