Amazon References

Friday, December 9, 2016

Primary Excel Parser


A simple excel parser that parses every row to an associative array.
When to use this?



  • Your excel file has the header on the first row, and records on the leisure rows.
  • You want to map the header titles to database keys.
  • You wish to map the information values to database enums.
  • example
    You need to parse an excel file with this format.
    identify Age Gender mobilephone 1 cell 2 invoice 18 Male 222-222-2222 777-777-7777 Peter 25 Male 222-333-4444 555-666-7777 Monica 31 female 666-666-6666 555-555-5555
    and you wish to map the header titles to database desk fields, and map genders to 'm' or 'f'.
    $parser = new Jhesyong\Excel\Parser; // Map 'identify' to 'name', 'Age' to 'age', and 'Gender' to 'gender'. $parser->addHeader('identify', 'name'); $parser->addHeader('Age', 'age'); // Map the values: 'm' is for 'Male', and 'f' is for 'female' $parser->addHeader('Gender', 'gender')->withOptions(['m' => 'Male', 'f' => 'Female']); // you can use a daily expression and move a callback to map numerous header titles. // in this case, 'cell 1' may be mapped to 'phone_1' $parser->addHeader('/^cell \d+$/', characteristic($title) return str_replace(' ', '_', strtolower($title)); ); // Load the file and start to parse. flow a callback feature // and it will be called for each row. $parser->loadFile('filename.xlsx')->parse(characteristic($information) // information could be an associative array. var_dump($statistics); ); FAQ I actually have distinctive titles of the equal textual content, but I are looking to map them to distinct database table fields.
    you can add headers with the same title however distinctive box names within the desired order.
    // the first 'phone' should be mapped to 'phone_1', // and the 2d 'cell' can be mapped to 'phone_2' $parser->addHeader('phone', 'phone_1'); $parser->addHeader('phone', 'phone_2'); I just want to use the title as the container name
    that you would be able to omit the 2nd argument.
    // 'cellphone' can be mapped to 'mobile' $parser->addHeader('phone'); I need to pre-define the header mapping and the alternate options.
    that you would be able to prolong the Parser and write your rules in the constructor.


    Links and related books

    No comments:

    Post a Comment