Home > Backend Development > PHP Tutorial > How to splice strings without duplication?

How to splice strings without duplication?

WBOY
Release: 2016-07-06 13:53:29
Original
1353 people have browsed it

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:

<code class="SQL">SELECT lei, shuxing, miaoshu FROM animal
WHERE lei IN ('大象', '熊');</code>
Copy after login

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

<code class="JSON">{
    "大象": {
        "眼睛": ["大大的", "圆圆的"],
        "鼻子": ["高高的"],
        "腿": ["长长的"],
        "嘴": ["小小的"]
    },
    "熊": {
        ...
    }
}</code>
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
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template