Table of Contents
Reply content:
Home Backend Development PHP Tutorial How to splice strings without duplication?

How to splice strings without duplication?

Jul 06, 2016 pm 01:53 PM
json mysql php

For example, there is the following data table structure
id lei shuxing miaoshu
1 Elephant has big eyes
2 Elephant has round eyes
3 Elephant has a tall trunk
4 Bear The nose is small
5 The elephant has long legs
6 The bear has thin legs
7 The elephant has a small mouth

The second, third and fourth columns may have n multiple situations. Find a PHP method that can achieve the following combination of requirements. The requirement is that each description is a lei, covering 3 items in shuxing ( Count as many as there are less than three items (such as bears) to generate different descriptions of an animal

Example: The result based on the above data should be

The elephant has big eyes, a high trunk, and long legs
The elephant has round eyes, a high trunk, and long legs
The elephant has big eyes, a tall trunk , with a small mouth
. . .
Since there are so many combinations, it’s impossible to list them all. I hope friends can understand through the above examples

Reply content:

For example, there is the following data table structure
id lei shuxing miaoshu
1 Elephant has big eyes
2 Elephant has round eyes
3 Elephant has a tall trunk
4 Bear The nose is small
5 The elephant has long legs
6 The bear has thin legs
7 The elephant has a small mouth

The second, third and fourth columns may have n multiple situations. Find a PHP method that can achieve the following combination of requirements. The requirement is that each description is a lei, covering 3 items in shuxing ( Count as many as there are less than three items (such as bears) to generate different descriptions of an animal

Example: The result based on the above data should be

The elephant has big eyes, a high trunk, and long legs
The elephant has round eyes, a high trunk, and long legs
The elephant has big eyes, a tall trunk , with a small mouth
. . .
Since there are so many combinations, it’s impossible to list them all. I hope friends can understand through the above examples

Builder pattern http://www.runoob.com/design-pattern/builder-pattern.html

<code>select lei,group_concat(concat(shuxing,miaoshu)) result from tb group by lei</code>
Copy after login

This will return
lei result
The elephant has big eyes, a tall nose, long legs, and a small mouth
The bear has a small nose and thin legs
...
Go back to the background to split the result by commas, and then randomly splice three results
---The above personal opinions, comments are welcome---

Actually, I still don’t quite understand whether the question is about php or sql.
But I feel that sql is not good at solving this kind of problem, so I assume it is a programming problem.

Since I don’t understand PHP, I only provide problem-solving ideas:
1. Query all qualified data according to lei, for example:

SELECT lei, shuxing, miaoshu FROM animal
WHERE lei IN ('大象', '熊');
Copy after login

2. Perform lei operations on the shuxing attribute and the group by attribute in the code, as follows:

{
    "大象": {
        "眼睛": ["大大的", "圆圆的"],
        "鼻子": ["高高的"],
        "腿": ["长长的"],
        "嘴": ["小小的"]
    },
    "熊": {
        ...
    }
}
Copy after login

3. Perform Cartesian product operation on the data in 2, done.

PS: In fact, the table structure is not designed well. It should be split into animal table (animal_id, animal_name) and animal_prop table (animal_prop_id, animal_id, prop_name, prop_desc).

You can use split tables and divide them into three tables. table1: used to record lei information table2: used to record shuxing, the ID of table1 table3:

Used to record the IDs of miaoshu and table2, and use three tables to perform related queries

Dear friends, can anyone help me solve this problem?

Yes, what I mentioned above is an idea
Split tables
and then perform related queries based on the tables
Take out the class separately and build a table
Create another table based on the attributes of the class (1 pair Multiple relationships)

python version:

<code>from collection import defaultdict

anims = defaultdict(lambda : defaultdict(lambda : defaultdict(set)))

for lei, shuxing, miaoshu in db.select('table', ['lei', 'shuxing', 'miaoshu']):
    anims[lei][shuxing].add(miaoshu)
    
# enum all descs
for lei, v1 in anims.items():
    print 'desc lei: ', lei
    shuxings = [(k, list(v)) for k,v in v1.items()]
    shuxing_size = len(shuxings)
    if shuxing_size == 0:
        pass
    elif shuxing_size == 1:
        for i in range(shuxing_size):
            for ii in range(shuxings[i][1]):
                print shuxings[i][0], shuxings[i][1][ii]
    elif shuxing_size == 2:            
        for i in range(shuxing_size):
            for j in range(i+1, shuxing_size):
                for ii in range(shuxings[i][1]):
                    for jj in range(shuxings[j][1]):
                        print shuxings[i][0], shuxings[i][1][ii], shuxings[j][0], shuxings[j][1][jj]
    else:
        for i in range(shuxing_size):
            for j in range(i+1, shuxing_size):
                for k in range(j+1, shuxing_size):
                    for ii in range(shuxings[i][1]):
                        for jj in range(shuxings[j][1]):
                            for kk in range(shuxings[k][1]):
                                print shuxings[i][0], shuxings[i][1][ii], shuxings[j][0], shuxings[j][1][jj], shuxings[k][0], shuxings[k][1][kk]</code>
Copy after login
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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles