How to splice strings without duplication?
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>
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 ('大象', '熊');
2. Perform lei
operations on the shuxing
attribute and the group by
attribute in the code, as follows:
{ "大象": { "眼睛": ["大大的", "圆圆的"], "鼻子": ["高高的"], "腿": ["长长的"], "嘴": ["小小的"] }, "熊": { ... } }
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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

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

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

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.
