-1

I have a php variable which reads data from CSV file using str_getcsv() function as below:

 $test = array_map('str_getcsv', file($path))

Now $test contains array as below:

 Array
(
  [0] => Array
    (
        [0] => Array
            (
                [0] => abc
            )

        [1] => Array
            (
                [0] => def
            )

        [2] => Array
            (
                [0] => ghi
            )

        [3] => Array
            (
                [0] => jkl
            )

        )
    )

I would like to convert it in simple array as follows:

 Array
 (
    [0] => abc
    [1] => def
    [2] => ghi
    [3] => jkl
 )

Can somebody help?

  • This has been asked before, please use the search. Don't ask for help, just search your question. – hakre Apr 03 '15 at 05:57
  • I have tried all those answers and then posted here. Simply dint ask for any help. – user3766186 Apr 03 '15 at 06:05
  • Which answers have you tried? Can you show the example what exactly you tried and how the outcome was and which part at the outcome / requirement is different from yours? You have to make that visible, all your tries, nice examples that are self containing, easy to reproduce, reference to existing material, explanations and all that stuff. – hakre Apr 03 '15 at 06:08
  • I have tried $result = call_user_func_array('array_merge', $array); and array_values..dint work in this case. – user3766186 Apr 03 '15 at 06:14
  • So your real question would be how to get the data out of a CSV file in form of an array? Why don't you use the FileObject or the file functions right ahead? – hakre Apr 03 '15 at 06:14
  • I have used file() in the code..Please see – user3766186 Apr 03 '15 at 06:16
  • And if you look at the duplicate question the array_merge answer is for two levels only, you've got three. This answer works with any depth: http://stackoverflow.com/a/1320259/367456 – hakre Apr 03 '15 at 06:16
  • I've seen you using file() however file() can't read CSV, it only reads a file line by line. But I was talking about parsing the CSV from the file directly. Read more here: http://stackoverflow.com/q/5455872/367456 – hakre Apr 03 '15 at 06:21

1 Answers1

2
$array = array_values($array);

try this

Vivek Singh
  • 2,453
  • 1
  • 14
  • 27