Table of Contents
Reply content:
Home Backend Development PHP Tutorial mysql - php excel import Import three-level classification How should the table be designed to get the desired data format?

mysql - php excel import Import three-level classification How should the table be designed to get the desired data format?

Aug 04, 2016 am 09:19 AM
excel mysql php sublime-text windows

mysql - php excel import Import three-level classification How should the table be designed to get the desired data format?

There are multiple first-level classifications with uncertain second-level classifications. Below the second-level classifications, there are uncertain third-level classifications. I want to get relevant data combinations and write them into the database, but I don’t know how to arrange this format to get the right data.
If you have time, please take a look and help. Thank you

Is there a way to get the value of the entire column and then distinguish 2 columns and then 3 columns based on the length

The data format obtained now is 1 2 3 by row. . .
Picture above
mysql - php excel import Import three-level classification How should the table be designed to get the desired data format?

Data printed on

<code>array (size=16)
  0 => 
    array (size=7)
      0 => float 1
      1 => string '销售部' (length=9)
      2 => string '销售1区' (length=10)
      3 => string '销售1科室' (length=13)
      4 => null
      5 => null
      6 => null
  1 => 
    array (size=7)
      0 => float 2
      1 => null
      2 => null
      3 => string '销售2科室' (length=13)
      4 => null
      5 => null
      6 => null
  2 => 
    array (size=7)
      0 => float 3
      1 => null
      2 => null
      3 => string '销售3科室' (length=13)
      4 => null
      5 => null
      6 => null
  3 => 
    array (size=7)
      0 => float 4
      1 => null
      2 => null
      3 => string '销售4科室' (length=13)
      4 => null
      5 => null
      6 => null
  4 => 
    array (size=7)
      0 => float 5
      1 => null
      2 => string '销售2区' (length=10)
      3 => string '销售1科室' (length=13)
      4 => null
      5 => null
      6 => null
  5 => 
    array (size=7)
      0 => float 6
      1 => null
      2 => null
      3 => string '销售2科室' (length=13)
      4 => null
      5 => null
      6 => null
  6 => 
    array (size=7)
      0 => float 7
      1 => null
      2 => null
      3 => string '销售3科室' (length=13)
      4 => null
      5 => null
      6 => null
  7 => 
    array (size=7)
      0 => float 8
      1 => null
      2 => null
      3 => string '销售4科室' (length=13)
      4 => null
      5 => null
      6 => null
  8 => 
    array (size=7)
      0 => float 9
      1 => string '市场部' (length=9)
      2 => string '市场1区' (length=10)
      3 => string '市场1科室' (length=13)
      4 => null
      5 => null
      6 => null
  9 => 
    array (size=7)
      0 => float 10
      1 => null
      2 => null
      3 => string '市场2科室' (length=13)
      4 => null
      5 => null
      6 => null
  10 => 
    array (size=7)
      0 => float 11
      1 => null
      2 => null
      3 => string '市场3科室' (length=13)
      4 => null
      5 => null
      6 => null
  11 => 
    array (size=7)
      0 => float 12
      1 => null
      2 => null
      3 => string '市场4科室' (length=13)
      4 => null
      5 => null
      6 => null
  12 => 
    array (size=7)
      0 => float 13
      1 => null
      2 => string '市场2区' (length=10)
      3 => string '市场1科室' (length=13)
      4 => null
      5 => null
      6 => null
  13 => 
    array (size=7)
      0 => float 14
      1 => null
      2 => null
      3 => string '市场2科室' (length=13)
      4 => null
      5 => null
      6 => null
  14 => 
    array (size=7)
      0 => float 15
      1 => null
      2 => null
      3 => string '市场3科室' (length=13)
      4 => null
      5 => null
      6 => null
  15 => 
    array (size=7)
      0 => float 16
      1 => null
      2 => null
      3 => string '市场4科室' (length=13)
      4 => null
      5 => null
      6 => null</code>
Copy after login
Copy after login

Reply content:

mysql - php excel import Import three-level classification How should the table be designed to get the desired data format?

There are multiple first-level classifications with uncertain second-level classifications. Below the second-level classifications, there are uncertain third-level classifications. I want to get relevant data combinations and write them into the database, but I don’t know how to arrange this format to get the right data.
If you have time, please take a look and help. Thank you

Is there a way to get the value of the entire column and then distinguish 2 columns and then 3 columns based on the length

The data format obtained now is 1 2 3 by row. . .
Picture above
mysql - php excel import Import three-level classification How should the table be designed to get the desired data format?

Data printed on

<code>array (size=16)
  0 => 
    array (size=7)
      0 => float 1
      1 => string '销售部' (length=9)
      2 => string '销售1区' (length=10)
      3 => string '销售1科室' (length=13)
      4 => null
      5 => null
      6 => null
  1 => 
    array (size=7)
      0 => float 2
      1 => null
      2 => null
      3 => string '销售2科室' (length=13)
      4 => null
      5 => null
      6 => null
  2 => 
    array (size=7)
      0 => float 3
      1 => null
      2 => null
      3 => string '销售3科室' (length=13)
      4 => null
      5 => null
      6 => null
  3 => 
    array (size=7)
      0 => float 4
      1 => null
      2 => null
      3 => string '销售4科室' (length=13)
      4 => null
      5 => null
      6 => null
  4 => 
    array (size=7)
      0 => float 5
      1 => null
      2 => string '销售2区' (length=10)
      3 => string '销售1科室' (length=13)
      4 => null
      5 => null
      6 => null
  5 => 
    array (size=7)
      0 => float 6
      1 => null
      2 => null
      3 => string '销售2科室' (length=13)
      4 => null
      5 => null
      6 => null
  6 => 
    array (size=7)
      0 => float 7
      1 => null
      2 => null
      3 => string '销售3科室' (length=13)
      4 => null
      5 => null
      6 => null
  7 => 
    array (size=7)
      0 => float 8
      1 => null
      2 => null
      3 => string '销售4科室' (length=13)
      4 => null
      5 => null
      6 => null
  8 => 
    array (size=7)
      0 => float 9
      1 => string '市场部' (length=9)
      2 => string '市场1区' (length=10)
      3 => string '市场1科室' (length=13)
      4 => null
      5 => null
      6 => null
  9 => 
    array (size=7)
      0 => float 10
      1 => null
      2 => null
      3 => string '市场2科室' (length=13)
      4 => null
      5 => null
      6 => null
  10 => 
    array (size=7)
      0 => float 11
      1 => null
      2 => null
      3 => string '市场3科室' (length=13)
      4 => null
      5 => null
      6 => null
  11 => 
    array (size=7)
      0 => float 12
      1 => null
      2 => null
      3 => string '市场4科室' (length=13)
      4 => null
      5 => null
      6 => null
  12 => 
    array (size=7)
      0 => float 13
      1 => null
      2 => string '市场2区' (length=10)
      3 => string '市场1科室' (length=13)
      4 => null
      5 => null
      6 => null
  13 => 
    array (size=7)
      0 => float 14
      1 => null
      2 => null
      3 => string '市场2科室' (length=13)
      4 => null
      5 => null
      6 => null
  14 => 
    array (size=7)
      0 => float 15
      1 => null
      2 => null
      3 => string '市场3科室' (length=13)
      4 => null
      5 => null
      6 => null
  15 => 
    array (size=7)
      0 => float 16
      1 => null
      2 => null
      3 => string '市场4科室' (length=13)
      4 => null
      5 => null
      6 => null</code>
Copy after login
Copy after login

You don’t need me to teach you how to read the data in Excel into a two-dimensional array. Let’s go directly to the code.

<code><?php

$lines = [
    ['销售部', '销售1区', '销售1科室'],
    ['', '', '销售2科室'],
    ['', '', '销售3科室'],
    ['', '', '销售4科室'],
    ['', '销售2区', '销售1科室'],
    ['', '', '销售2科室'],
    ['', '', '销售3科室'],
    ['', '', '销售4科室'],
    ['市场部', '市场1区', '市场1科室'],
    ['', '', '市场2科室'],
    ['', '', '市场3科室'],
    ['', '', '市场4科室'],
    ['', '市场2区', '市场1科室'],
    ['', '', '市场2科室'],
    ['', '', '市场3科室'],
    ['', '', '市场4科室'],
];

$formatedData = [];
foreach ($lines as $line) {
    if (!empty($line[0])) {
        $level1 = $line[0];
        $formatedData[$level1] = [];
    }
    if (!empty($line[1])) {
        $level2 = $line[1];
        $formatedData[$level1][$level2] = [];
    }
    if (!empty($line[2])) {
        $level3 = $line[2];
        $formatedData[$level1][$level2][] = $level3;
    }
}

print_r($formatedData);</code>
Copy after login

The result of running the program is:

Array
(

<code>[销售部] => Array
    (
        [销售1区] => Array
            (
                [0] => 销售1科室
                [1] => 销售2科室
                [2] => 销售3科室
                [3] => 销售4科室
            )

        [销售2区] => Array
            (
                [0] => 销售1科室
                [1] => 销售2科室
                [2] => 销售3科室
                [3] => 销售4科室
            )

    )

[市场部] => Array
    (
        [市场1区] => Array
            (
                [0] => 市场1科室
                [1] => 市场2科室
                [2] => 市场3科室
                [3] => 市场4科室
            )

        [市场2区] => Array
            (
                [0] => 市场1科室
                [1] => 市场2科室
                [2] => 市场3科室
                [3] => 市场4科室
            )

    )
</code>
Copy after login

)

My method starts from the classification column and marks cat1, cat2, cat3, and then uses a for loop in the background to come out

It is correct to obtain the data in the desired format according to the adopted method. However, it is still troublesome to judge some of the data being operated, or it may not be possible to judge. For example, it is impossible to judge whether its parent exists in a three-level classification. I can only judge whether there is a subset, then add a new one. Overall, I feel that the format of this requirement still needs to be adjusted. There are some problems. I may not be able to use this format because the internal use requirements are not so strict.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

PHP: Is It Dying or Simply Adapting? PHP: Is It Dying or Simply Adapting? Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

phpmyadmin connection mysql phpmyadmin connection mysql Apr 10, 2025 pm 10:57 PM

How to connect to MySQL using phpMyAdmin? The URL to access phpMyAdmin is usually http://localhost/phpmyadmin or http://[your server IP address]/phpmyadmin. Enter your MySQL username and password. Select the database you want to connect to. Click the "Connection" button to establish a connection.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

H5: Tools, Frameworks, and Best Practices H5: Tools, Frameworks, and Best Practices Apr 11, 2025 am 12:11 AM

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

See all articles