Find exact same string in multiple strings

后端 未结 2 1658
情深已故
情深已故 2021-01-22 15:51

The following example.

I have an array like this :

Array
(
    [0] =>  vlakke lasflenzen PN6
    [1] =>  vlakke lasflenzen PN10
    [2] =>  vlak         


        
2条回答
  •  面向向阳花
    2021-01-22 16:41

    I finally got my own solutin to work.

    $names = array[
        'vlakke lasflenzen PN6',
        'vlakke lasflenzen PN10',
        'vlakke lasflenzen PN16',
        'vlakke lasflenzen PN25-40'
    ];
    
    $name = [];
    $parts = [];
    foreach ($names as $name) {
        $parts[] = array_filter(explode(' ', $name));
    }
    
    foreach ($parts[0] as $index => $part) {
            $match = true;
            foreach ($parts as $item) {
                    if ($item[$index] !== $part)
                            $match = false;
            }
            if ($match)
                    $name[] = ucfirst($part);
    }
    
    
    echo implode(' ', $name);
    # Output : "Vlakke Lasflenzen"
    

提交回复
热议问题