Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8472421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:04:08+00:00 2026-06-10T17:04:08+00:00

This code actually works but it’s a little bit too verbose, i would like

  • 0

This code actually works but it’s a little bit too verbose, i would like to shrink it but i can’t find a solution…

Edit: further explanation: $keywords is an array with a lot of single words extracted from a text. In $text i would like to put al lemmas i can create using adjacent words. Let’s say my original text is “I like green apples”, in $keywords i have “I”,”like”,”green”,”apples”. In $text i will have then “I like”, “I like green”, “I like green apples” …

for ($i=0;$i<=count($keywords);$i++) {
        $text[] = $keywords[$i];
        $text[] = $keywords[$i]." ".$keywords[$i+1];
        $text[] = $keywords[$i]." ".$keywords[$i+1]." ".$keywords[$i+2];
        $text[] = $keywords[$i]." ".$keywords[$i+1]." ".$keywords[$i+2]." ".$keywords[$i+3];
        $text[] = $keywords[$i]." ".$keywords[$i+1]." ".$keywords[$i+2]." ".$keywords[$i+3]." ".$keywords[$i+4];
        $text[] = $keywords[$i]." ".$keywords[$i+1]." ".$keywords[$i+2]." ".$keywords[$i+3]." ".$keywords[$i+4]." ".$keywords[$i+5];
        $text[] = $keywords[$i]." ".$keywords[$i+1]." ".$keywords[$i+2]." ".$keywords[$i+3]." ".$keywords[$i+4]." ".$keywords[$i+5]." ".$keywords[$i+6];    
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:04:10+00:00Added an answer on June 10, 2026 at 5:04 pm

    You will have to use two for-loops in order to create such a list:

    <?php
    
    $keywords = explode(' ', 'k1 k2 k3 k4 k5 k6 k7');
    $text = array();
    
    for ($i = 0; $i < count($keywords); $i++) {
            $line = array();
    
            for ($j = 0; $j <= $i; $j++) {
                    $line[] = $keywords[$j];
            }
    
            $text[] = implode(' ', $line);
    }
    
    print_r($text);
    
    /*
    Array
    (
        [0] => k1
        [1] => k1 k2
        [2] => k1 k2 k3
        [3] => k1 k2 k3 k4
        [4] => k1 k2 k3 k4 k5
        [5] => k1 k2 k3 k4 k5 k6
        [6] => k1 k2 k3 k4 k5 k6 k7
    )
    */
    

    This code should work with arbitrary length array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the code I have, it actually works, not perfectly but it does,
I have seen code like this (actually seeing another person type it up): catch
I have something like this in my code. mystring.Split(new[]{/,*}, StringSplitOptions.RemoveEmptyEntries); however, what I actually
This following code snippet works fine using jQuery1.2.3, but it doesn’t work with latest
The following jQuery code actually works fine in MS IE8 but returns ‘undefined’ in
Note: This code actually codes from a tutorial book I'm reading at the moment,
Kind of need help understanding what this code actually outputs. Does it out put
For some reason, this code does not actually draw my bitmap file... or show
In this code fragment, which constructor is actually called? Vector v = getVector(); Vector
I saw this code snippet during our lab and it actually compiles in MSVC2008

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.