Find length of initial segment matching mask on Arrays

后端 未结 2 1930
鱼传尺愫
鱼传尺愫 2021-01-23 07:18

Given an array with n values, for example:

$arr[] = \'ABCDEFABC\';
$arr[] = \'ABCDEFDEF\';
$arr[] = \'ABCDEFGHI\';
$arr[] = \'ABCDEFJKL\';

how

2条回答
  •  不知归路
    2021-01-23 07:50

    I've come up with a solution for my first problem:

    EDIT: BUGGY!

    $arr = array();
    
    // Bug: ABCDEFX
    
    $arr[] = 'ABCDEFAXC';
    $arr[] = 'ABCDEFDXF';
    $arr[] = 'ABCDEFGXI';
    $arr[] = 'ABCDEFJXL';
    
    /*
    $arr[] = 'ABCDEFABC';
    $arr[] = 'ABCDEFDEF';
    $arr[] = 'ABCDEFGHI';
    $arr[] = 'ABCDEFJKL';
    */
    
    // ABCDEF    
    $result = implode('', call_user_func_array('array_intersect_assoc', array_map('str_split', $arr)));
    

    One left to go now...

提交回复
热议问题