1

I tried to export csv files so I got an array like

Array
(
    [0] => Name
    [1] => Emp Code
    [2] => Designation
    [3] => Basic
    [4] => DA
    [5] => HRA
    [6] => CCA
    [7] => Conveyance
    [8] => Others
    [9] => Mgt Contr PF
    [10] => PL Encashment
    [11] => Income Tax
    [12] => Prof Tax
    [13] => Welfare Fund
)
Array
(
    [0] => Li
    [1] => 60
    [2] => Web Designer
    [3] => 14400
    [4] => 9600
    [5] => 4800
    [6] => 4800
    [7] => 1600
    [8] => 12800
    [9] => 800
    [10] => 0
    [11] => 0
    [12] => 0
    [13] => 20
)

Here first array values are the csv file column name, so I don't want to save them in database.

Here is my code

while (($data = fgetcsv($file, 1000, ",")) !== FALSE) {
                            $num = count($data);

                            for ($c=0; $c < $num; $c++) {
                                $importData_arr[$i][] = $data[$c];
                            }
                            $i++;
                        }
                          fclose($file);
                            $skip = 0;
                             foreach($importData_arr as $data){
                                 echo '<pre>';print_r($data);
                             }
        }

How can I remove column name from array. Please help me

3
  • 1
    you can simply use unset php.net/manual/en/function.unset.php such as unset($array[0]) Commented Feb 10, 2020 at 6:09
  • you can also try array_shift() w3schools.com/php/func_array_shift.asp Commented Feb 10, 2020 at 6:15
  • I don't understand you, but if you would like to combine the first array values as a key and the second array values as a value in one array you can use [array_combine] php.net/array_combine Commented Feb 10, 2020 at 6:48

2 Answers 2

3

You can skip the first row by reading it prior to the loop. You can also massively simplify your code:

fgets($file);
while (($data = fgetcsv($file, 1000, ",")) !== FALSE) {
    $importData_arr[] = $data;                        
}
fclose($file);
$skip = 0;
foreach($importData_arr as $data){
    echo '<pre>';print_r($data);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I totally missed that i can simplify OP's code itself to make it simple and get desired output. good catch.+1
0

use unset function:

unset($importData_arr[0]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.