PHP How to convert a list of strings (array) into a nested array based on string content

I have a list of strings with contained keywords.

//random keywords random order
$array = array(
    'Keyword7 keyword4 keyword9 keyword1'
    'keyword4 keyword9 Keyword7 keyword2'
    'Keyword7 keyword4 keyword9 keyword3'
    'keyword3 keyword9 Keyword7 keyword5'

);

I would like to find the / s keyword that exists on all lines "Keyword7" and Keyword9"and delete them. I would like to set these keywords as the key of my new array.

$new_array = array();
$new_array["Keyword7 Keyword9"] = array();

The value of this new array should now be the same, but now they do not have the parent keyword / s.

$new_array = array(
    'Keyword7 keyword9' =>array(
        array(
            ' keyword4 keyword1'
            ' keyword4 keyword2'
            ' keyword4 keyword3'
            ' keyword3 keyword5'
        );
    )
)

"keyword4" is the next match that has the most cases, so it will be next. and then the next best match is "keyword3" or "keyword5".

$new_array = array(
    'Keyword7 keyword9' =>array(
        'keyword4' => array(
            'keyword1'
            'keyword2'
            'keyword3'
        ),
        'keyword3' =>array(
            'keyword5'
        )
    )
)

The following lines are all the unique keywords that the array fills.

What has been done so far

prepared array

    $keywordlist_array = array();

    foreach ($keywordlist as $key => &$string) {

        $new_str = trim(preg_replace('/\s+/', ' ',$string),' ');
        $arr = explode(' ', $new_str);
        asort($arr);

        $keys = array_values($arr);

        $keywordlist_array[$key] = array_combine($keys, array_fill_keys($keys, 0));

        $string = implode(' ', $arr);
    }

, . .

, , .

    foreach ($keywordlist_array as $key_1 => $array_1) {

        foreach ($keywordlist_array as $key_2 => $array_2) {

            foreach (array_keys($array_1) as $keyword) {

                if(array_key_exists($keyword,$array_2)){

                    $keywordlist_array[$key_1][$keyword]++;
                }
            }
        }
    }

ive , , , .

    function keywordListToNestedArray($keywordlist_array){

        $new_array = array();

        $length = count($keywordlist_array);

        $all_share_keywords = false;
        $some_share_keywords = false;


        $keywords  = array();

        $new_keywordlist_array = array();

        $max_values = array();


        foreach ($keywordlist_array as $key => $arr) {

            if(in_array($length, $arr)){

                $all_share_keywords = true;

                if(!$keywords){

                    foreach ($arr as $keyword => $value) {

                        if($value == $length){
                            $keywords[] = $keyword;
                        }
                    }
                }
                if($keywords){

                    $new_keywordlist_array[$key] = array_diff_key($arr, array_flip($keywords));
                }


            } else {



            }
        }

        if($all_share_keywords){

            $keyword = implode(' ', $keywords);
            $new_array[$keyword] = keywordListToNestedArray($new_keywordlist_array );

        } else if($some_share_keywords){

            // will have multiple new parents


        } else {

            //all values equal 1 (one occurance)

            foreach ($keywordlist_array as $key => $keywords) {

                $new_array[$key] = implode(' ', array_keys($keywords));
            }
        }

        return $new_array;
    }

    $new_array = keywordListToNestedArray($keywordlist_array);

PHP, , - Similar_text() levenshtein(),

: " ?". , .

+4
1

, . , , . , .

Ex. :

([keyword1] = > Array ([keyword2] = > Array ([keyword3] = > Array ([0] = > keyword4 [1] = > keyword5 [2] = > keyword7 [3] = > keyword6 [4] = > keyword10 [5] = > keyword9 [6] = > keyword8))))

<?php
        $keywords = array(
            'keyword1 keyword2 keyword3 keyword4',
            'keyword1 keyword2 keyword3 keyword5',
            'keyword1 keyword2 keyword7 keyword6',
            'keyword1 keyword10 keyword9 keyword8'
        );

        //create a new array with all the keywords
        $keywordArray = [];
        foreach($keywords as $keyword){
            $pieces = explode(" ", $keyword);
            foreach($pieces as $piece){
                $keywordArray[] = $piece;
            }
        }
        //count each keyword appearence
        $keywordCount = array_count_values($keywordArray);

        //create associative array key names
        $max = count($keywords);
        $newArray = [];
        while($max >= 0){
            $myStr = "";
            foreach($keywordCount as $key => $value){
                if($value == $max){
                    $myStr .= $key." ";
                }
            }
            if($myStr != ""){
                $newArray[$myStr] = $max;
            }
            $max--;
        }

        //get final array data
        end($newArray);
        $values = explode(" ", key($newArray));
        foreach($values as $value){
            if($value != ""){
                $finalArray[] = $value;
            }
        }
        unset($newArray[key($newArray)]);
        reset($newArray);

        //add previous key and final data to it
        end($newArray);
        $testArray[key($newArray)] = $finalArray;
        $rKey = key($newArray);
        unset($newArray[key($newArray)]);
        reset($finalArray);

        //repeat proccess from bottom to top
        while(!empty($newArray)){
            end($newArray);
            $testArray[key($newArray)] = $testArray;
            unset($newArray[key($newArray)]);
            unset($testArray[key($testArray)]);
            reset($newArray);
        }
        unset($testArray[$rKey]);

print_r($testArray);

?>
0

Source: https://habr.com/ru/post/1608667/


All Articles