#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Get 2 words in every value of array


    Hi i have code like this one :
    Code:
     $words2= 'If you want to have a preformatted block within a list, indent by eight spaces.';  $forbiddenwords=array("word1","word2");
     foreach($words2 as $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 i want is to creat an array that every value of it contain 2 words from the string. the code above dosn't work -implode() not working with associative arrays- also the result that i'm trying to have is like that

    Code:
    array ( 
    $words2[0]=>'If you'
     $words2[1]=>'you want' 
    $words2[1]=>'want to'
     ... )
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,007
    Rep Power
    9398
    Use str_word_count + array_chunk.

    Comments on this post

    • codergeek42 agrees : (No rep for now, sorry...)

IMN logo majestic logo spyfu logo threadwatch logo seochat tools logo