#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    232
    Rep Power
    97

    Finding the source of array_combine error


    SOLVED!
    Sorry, as I was working on a different "column can't be null" error I found a cell that had a carriage return followed by the cell's data. Removed the carriage return and both errors disappeared...

    I get this error TWICE:
    PHP Code:
    Warningarray_combine(): Both parameters should have an equal number of elements in C:\xampp\htdocs\DCI\DCISession.php on line 24 
    PHP Code:
    function csv_to_array($filename$delimiter=','$enclosure='"'$escape '\\') {

        if(!
    file_exists($filename) || !is_readable($filename)) {echo "File does not exist or is not readable."; return false;}

        
    $header null;
        
    $values = array();
        
    $data = array();
        
    $lines file($filename);

         foreach(
    $lines as $line) {
            
    $values str_getcsv($line$delimiter$enclosure$escape);
            
    $values array_map('trim'$values);
            if(!
    $header) {$header $values;
    /*        var_dump($header);
            echo "<br><br>";
            var_dump($values);
            echo "<br><br>";
    */        
    }
            else 
    $data[] = array_combine($header$values); /* line 24 */
          
    }

        return 
    $data;

    I'm working with a CSV file that I've been using to process data for some time. I haven't added or removed any columns but if I did there would be values in the new column.

    Additionally, when I print_r the resultant array it's got the correct number of rows and does not appear to be missing anything.

    How do I track down the rows or cells that is jacking this up?
    Last edited by big0mike; January 3rd, 2018 at 10:42 AM. Reason: Problem solved
  2. #2
  3. No Profile Picture
    Super Moderator
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,535
    Rep Power
    112
    I'm assuming in your test example above, when you print_r() the 2 arrays ($header & $values), they both offer the same amount of values in the returned single-level array?

    If so, would it be possible to provide an example of the print_r() results from each array?


    I just noticed that $header is set to NULL, and nothing more. array_combine() will fail with this since the 2 arrays need to match in count.

    Edit: And I see you just caught that. Good job!
    Last edited by Triple_Nothing; January 3rd, 2018 at 10:46 AM.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    232
    Rep Power
    97
    I didn't know the two were connected nor the cause but I was searching for the NULL content by echoing each line in the for loop and went to the row where it took a dump. Actually took me a little bit to notice that the carriage return was there...

IMN logo majestic logo threadwatch logo seochat tools logo