Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6967577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:20:23+00:00 2026-05-27T16:20:23+00:00

I’m trying to create a INSERT statement for every row in a PHPExcel object.

  • 0

I’m trying to create a INSERT statement for every row in a PHPExcel object. As I’ve struggled to iterate across the column (ie. go B1 C1 D1, get the values, and put them into an array), I’ve opted to get all the values for each column and put them into a multi-dimensional array that looks like this:

Array
(
    [foo] => Array
        (
            [0] => 250
            [1] => 247
            [2] => 279
            [3] => 249
        )

    [bar] => Array
        (
            [0] => AM PROV
            [1] => AM PROV
            [2] => AM PENS
            [3] => AM PROV
        )

    [schoo] => Array
        (
            [0] => xxxx
            [1] => yyy
            [2] => zzz
            [3] => aaa
        )
)

I want to merge each of the arrays so that all the data at index 0 is in one array, etc. I’ve built a generic tool to allow you to select the columns you want from an uploaded spreadsheet. It needs to first merge the column data into a single array and then it should generate INSERT statements for each of the arrays. So the final deliverable is this:

INSERT INTO (foo, bar, schoo) VALUES (250, "AM PROV", "xxxx");

All help appreciated.

UPDATE: Hey all, thank you very much for your answers. I finally managed to get it working using row and cell iterators as per Mark’s suggestion and it’s working. I have a separate issue with it now, but I think it’s something I can solve. Thanks again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:20:24+00:00Added an answer on May 27, 2026 at 4:20 pm
    <?php
    $uberArray = array(
        "foo" => array(
            0 => 250,
            1 => 247,
            2 => 279,
            3 => 249,
        ),
        "bar" => array(
            0 => "AM PROV",
            1 => "AM PROV",
            2 => "AM PENS",
            3 => "AM PROV",
        ),
        "schoo" => array(
            0 =>  "xxxx",
            1 =>  "yyy",
            2 =>  "zzz",
            3 =>  "aaa",
        )
    );
    
    $yourMysqlLink = mysql_connect('localhost', 'user', 'pass');
    mysql_query('SET NAMES utf8'); // Adjust according to your encoding
    
    $colNames = array_keys($uberArray);
    $stringCols = array('bar', 'schoo');
    $sqlInsertStr = 'INSERT INTO `your_table` (`'.implode('`, `', $colNames)."`) VALUES \n";
    
    $rows = array();
    // Not really for iterating the first array, we just need a loop
    foreach ($uberArray[$colNames[0]] as $k => $v) {
        $vals = array();
        foreach ($colNames as $v2) {
            $val = $uberArray[$v2][$k];
            if (in_array($v2, $stringCols)) {
                $val = "'".mysql_real_escape_string($val, $yourMysqlLink)."'";
            }
            $vals[] = $val;
        }
        $rows[] = "\t(".implode(', ', $vals).")";
    }
    $sqlInsertStr .= implode(",\n", $rows).';';
    
    echo '<pre style="clear:both;">'.$sqlInsertStr.'</pre>'; ;
    

    Note that you may need to do a few adjustments for performance reasons, if $uberArray is big (e.g. splitting the insert string into chunks). Or you can convert the data to CSV and use MySQL LOAD DATA INFILE method, which is real fast.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.