php - Get 2 words in every array value -


i have code 1

$words2= 'if want have preformatted block within list, indent 8 spaces.';  $forbiddenwords=array("word1","word2");  foreach($words2 $b=>$v) {     if(in_array($v, $forbidden) ){     unset($words2[$b] );      } }  $words2 = array_values($words2);  $words2=implode(' ',$words2);  $words2 = implode(' ',array_chunk(mb_split('\s', $words2), 2));  echo "<pre>"; print_r($words2); echo "</pre>"; 

what want create array every value of contain 2 words string. code above doesn't work -implode() not working associative arrays- result i'm trying have that

array ( $words2[0]=>'if you' $words2[1]=>'you want' $words2[1]=>'want to' ... ) 

you over-complicating things, both when removing blacklisted words , when constructing arrays of pairs of words.

to remove blacklisted:

$inputwords = mb_split('\s+', 'if want have preformatted block...'); $forbiddenwords = array("want", "have");  $filtered = array_diff($inputwords, $forbiddenwords); // removes blacklisted 

to join every 2 filtered words string:

$pairs = array_chunk($filtered, 2); // array of arrays $pairs = array_map(function($a) { return implode(' ', $a); }, $pairs); 

see in action.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -