在php

时间:2016-05-08 22:48:19

标签: php arrays database laravel

我想从用户那里获取输入文本。我想将用户使用的单词与也具有值的单词DB进行比较。

现在我创建了一个数组,它将输入运行到DB并返回值。我遇到的问题是它的顺序不正确。

    // GETTING THE ARRY OF WORDS FROM entry_body( $entry_text)
    $entry_text = $request->only('entry_body');

    // REMOVE THE KEY AND JUST GET THE STRING VALUE
    $entry_text = array_pull($entry_text,"entry_body");
    $entry_noTag = strip_tags($entry_text);

    // CONVERTE THE STRING ARRAY
    $entry_explode = explode(" ", $entry_noTag);

    // COUNT THE LENTH OF THE WORDS AND DIVIDE IT BY 3
    $entry_lenth = count($entry_explode);

    $new_array = array();
    // EACH WORD GETS ANALAYZED 
    $matched_words = Words::whereIn('word', $entry_explode)->get();
    foreach($matched_words as $mw){
            $new_array[]=$mw->word;
            $value_array[$mw->word]=$mw->value;
    }

    $arrayOfWords = array();
    foreach ($entry_explode as $word) {
        if (in_array($word,$new_array)) {
            $arrayOfWords['yes_words'][] = $word;
        } else {
            $arrayOfWords['no_word'][] = $word;
        }
    }

我还需要按顺序将这些单词分成3组。我这样做是为了让小组按正确的顺序排列。

    $entry_math = number_format($entry_lenth / 3);


    return array_chunk($entry_explode, $entry_math);

这是我得到的输出

     [
      [
        "Hey",
        "Jude,",
        "don't",
        "make",
        "it",
        "bad",
        "Take",
        "a",
        "sad",
        "song"
       ],
     [
        "and",
        "make",
        "it",
        "better",
        "Remember",
        "to",
        "let",
        "her",
        "into",
        "your"
       ],
       [
        "heart",
        "Then",
        "you",
        "can",
        "start",
        "to",
        "make",
        "it",
        "better."
       ]
      ]

我需要用$ value_array中的数字替换这些单词,但我不能100%确定应该怎么做。

这就是$ value_array的样子。

 {
   "a": "0",
   "and": "0",
   "bad": "1",
   "better": "2",
   "can": "2",
   "heart": "3",
   "her": "3",
   "hey": "3",
   "into": "0",
   "it": "2",
   "let": "1",
   "make": "1",
   "remember": "3",
   "sad": "1",
   "song": "2",
   "start": "3",
   "take": "1",
   "then": "2",
   "to": "2",
   "you": "2",
   "your": "2"
 }

1 个答案:

答案 0 :(得分:1)

你可以用简单的foreach解决它,

见这个例子,

考虑这个数组。

$input = ['hi', 'you', 'me', 'and','others'];

$database_input = [
    'hi' => 1,
    'you' => 5,
    'me' => 4,
    'and' => 3,
    'others' => 2,
    'some' => 6,
    'extra' => 7,
    'elements' => 8
];

获取具有此输出的数组

Array
(
    [hi] => 1
    [you] => 5
    [me] => 4
    [and] => 3
    [others] => 2
)

您需要做的就是,

$input = ['hi', 'you', 'me', 'and','others'];

$database_input = [
    'hi' => 1,
    'you' => 5,
    'me' => 4,
    'and' => 3,
    'others' => 2,
    'some' => 6,
    'extra' => 7,
    'elements' => 8
];

$output = [];

foreach($input as $item) {
    if(array_key_exists(strtolower($item), $database_input))
        $output[strtolower($item)] = $database_input[strtolower($item)];
}

以下是完成工作的基本逻辑。

我已经创建了适合您输入的php粘贴,请参阅下面的链接:

http://sandbox.onlinephpfunctions.com/code/63c1a0eb669df43a92a15104340f528a76a2cfb0