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 7936697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:17:35+00:00 2026-06-03T22:17:35+00:00

I have a CSV file to be imported to MYSQL database. The problem is

  • 0

I have a CSV file to be imported to MYSQL database.
The problem is that each line has different number of fields.
and their arrangement is rather random.
However, they can be divided into categories (text,images….etc).

In current form it’s too irregular for direct import to the database, so I tired to re-format it using PHP.

simplified example:

$messy = array( 
         [0]  => array("text1","text2" ),
         [1]  => array("img1.jpg" ),
         [2]  => array("text1","img1.jpg","img2.jpg"),
         [3]  => array("img1.jpg","text1","text2"),
            ...
         [1000]  => array("text2","img1.jpg","img2.jpg","text1")
         ); 

i would like the new array to match the following the pattern

array( 
         [0]  =>array(TEXT,TEXT,IMG,IMG),
         [1]  =>array(TEXT,TEXT,IMG,IMG),

in case of not sufficient data the remaining values=0;

         [0]  =>array("text1","text2",0,0),
         [1]  =>array(0,0,"img1.jpg",0),

i’ve tried to create new array and move “text” fileds to the begining like this:

$ordered=array();            
$i=0;

    foreach($messy as $row){

        foreach($row as $item){
            if (strlen(strstr($item,"text"))>0) {

                    if(($key = array_search($item, $row)) !== false){                   
                        unset($row[$key]);                      
                    }

                array_unshift($row,$item);

            }

        }


        $ordered[$i++]=$row;

    }

However it doesn’t re-format, just reorder.

  • 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-06-03T22:17:37+00:00Added an answer on June 3, 2026 at 10:17 pm
    function item_is_img($item) {
        $ext = pathinfo( $item, PATHINFO_EXTENSION )
        switch ( strtolower($ext) ) {
            case 'jpg':
            case 'jpeg':
            case 'png':
                return true;
                break;               
            default:
                return false;
        }        
    }
    
    function reorder( $inputArray) {
        $resultArray = array();
        foreach ($inputArray as $line) {
            $t = 0;
            $i = 2;
            $result = array(0,0,0,0);
            foreach ($line as $item) {
                if ( item_is_img($item) )
                    $result[$i++] = $item;
                else
                    $result[$t++] = $item;
            }
            $resultArray[] = $result;
        }
        return $resultArray;
    }
    

    Completely untested. Assumes there are at most 2 TEXT types and 2 IMG types. Prone to failure.

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

Sidebar

Related Questions

I have a MySQL table that I have imported from a CSV file. In
I have a CSV file with several entries, and each entry has 2 unix
I have a very large CSV file that I imported into a sqlite table.
I have the following code for uploading a csv file to a mysql database.
I have csv file with a line that looks something like this: ,,,,,,,,,, That's
I have a csv file that has a date field in a format like
I have been handed a very large CSV file that needs to be imported
I have .csv file that contain 2 columns delimited with , . file.csv word1,word2
i have csv files, java app and database, i read csv file from my
I have a CSV file which has the following format: id,case1,case2,case3 Here is a

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.