Retrieving column names along with their values as an array while having the ability to change the column name. - PHP Excel

StackOverflow https://stackoverflow.com/questions/23420383

  •  13-07-2023
  •  | 
  •  

Question

I have this data on my csv file

enter image description here

I was wondering if there would be any possibility to retrieve the data as an array using loop by the use of PHP Excel's functionality.

Like was shown below:

Note: I manually input the values not by retrieving the data from csv file.

Array (
    [0] => Array
        (
            [Email Address] => email11@gmail.com
            [First Name] => Guy 11
            [Last Name] => Stand 11
        )

    [1] => Array
        (
            [Email Address] => email12@gmail.com
            [First Name] => Guy 12
            [Last Name] => Stand 12
        )

    [2] => Array
        (
            [Email Address] => email13@gmail.com
            [First Name] => Guy 13
            [Last Name] => Stand 13
        )
)

And by any chance if there were any functionality to change the column name, so it would be like

    [0] => Array
        (
            [Sample New Column Name Hurray] => email11@gmail.com
            [New Colum Name] => Guy 11
            [Last Name] => Stand 11
        )

Any help would be apprecited.

Was it helpful?

Solution

Here are the steps that you need to follow in order to solve your problem:

  • Use str_getcsv to parse csv string into array.

  • Use for loop to iterate through the array that you will get from step-1.

  • Within for loop, change the key to what you want. You can do this easily by making entirely new array with the keys you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top