1

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] => [email protected]
        [3] => NY
    )
[2] => Array
    (
        [0] => Bar
        [1] => Bar & co
        [2] => [email protected]
        [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] => [email protected]
   )
 [1] => Array
 (
    ['Name'] => Bar
    ['Company'] => Bar & co
    ['Email] => [email protected]
 )
)

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

5
  • 2
    Perhaps it is better to show us how you read the CSV file. Perhaps it can be read correctly right from the start? Commented Jul 22, 2019 at 14:50
  • 1
    Can we see at least one of your attempts Commented Jul 22, 2019 at 14:50
  • I use SimpleXLSX php class for read excel and csv files Commented Jul 22, 2019 at 14:59
  • Ah, so it's not a simple CSV file? If it is a CSV file then SimpleXLSX is a bit of an overkill. Like taking a plane to get bread. Commented Jul 22, 2019 at 15:00
  • @KIKOSoftware - Sorry, I thought it was not important the type of file I read but only the result of data obtained Commented Jul 22, 2019 at 15:01

2 Answers 2

2

You can try the below code. It will make header name and value of that dynamic, based on csv file.

$header = array();
foreach($array as $key=>$value){

    if($key == 0){
        $header = array_values($value);
    }else{

        $final_result[]= array_combine($header, array_values($value));
    }
}

echo "<pre>";
print_r($final_result);

DEMO

Sign up to request clarification or add additional context in comments.

Comments

1

Try and build what you want when you read the file, rather than having to rearrange something you didnt want in the first place.

// loose the titles line
fgetcsv($file, 1000, ',')

$rows= [];

while ( $line = fgetcsv($file, 1000, ',') !== FALSE) {
    $rows[] = ['Name'=> $line[0], 'Company'=> $line[1], 'Email'=> $line[2] ];
}

@patent pending @KIKO Software

2 Comments

I have a patent pending on this idea!
Full attribution given :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.