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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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 and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
