I read a csv file and got this array $rows
Array
(
[0] => Array
(
[0] => Name
[1] => Company
[2] => Email
[3] => City
)
[1] => Array
(
[0] => Foo
[1] => Foo & co
[2] => foo@mail.com
[3] => NY
)
[2] => Array
(
[0] => Bar
[1] => Bar & co
[2] => bar@mail.com
[3] => Las Vegas
)
)
I would try to transform it into an associative array by filtering only some fields contained in the first array. The result should be this
Array
(
[0] => Array
(
['Name'] => Foo
['Company'] => Foo & co
['Email] => foo@mail.com
)
[1] => Array
(
['Name'] => Bar
['Company'] => Bar & co
['Email] => bar@mail.com
)
)
Unfortunately I tried it in various ways but without success.
$name = array_search('Name', $rows[0]);
$company = array_search('Company', $rows[0]);
$email = array_search('Email', $rows[0]);
$rows = unset($rows[0]);
$array = array();
foreach ( $rows as $r => $row ) {
foreach ( $row as $c => $cell ) {
if ($c == $name) { $array = array( 'name' => $cell ); }
if ($c == $company) { $array = array( 'company' => $cell ); }
if ($c == $email) { $array = array( 'email' => $cell ); }
}
}
Can you give me some suggestions? Thank you