开发者

How do I create substrings from an array using PHP?

I have an array of data that looks like this:

2008, Honda, Accord, Used, Car

And I'm trying to figure a way to make a number of sub strings from each item in the array. For example, I would like to loop each item and create the following substrings:

2008
2008 Honda
2008 Accord
2008 Used
2008 Car
2008 Honda Accord
2008 Honda Used
2008 Honda Car
2008 Accord Honda
2008 Accord Used
2008 Accord Car
2008 Used Honda
2008 Used Accord
2008 Used C开发者_如何学Goar
2008 Car Honda
2008 Car Accord
2008 Car Used
Honda
Honda 2008
Honda Accord
Honda Used
Honda Car
Honda 2008 Accord
Honda 2008 Used
etc ...

I need to make sure that there are no duplicates created and I need to prevent it from adding the same word twice (ex: Honda Honda OR 2008 Honda 2008 - i dont want that). Has anyone wrote anything like this or know where I can find a script that works the same way?


How about this:

<?php

$vals = array('2008', 'Honda', 'Accord', 'Used', 'Car');

function make_keyword_strings($array, $maxdepth = false, $prefix = '') {
  $ret = array();
  // if we are tracking depths, subtract one from the depth:
  $newdepth = $maxdepth === false ? false : $maxdepth-1;
  if ($newdepth < 0) return $ret; // we went over our depth
  foreach($array as $key => $value) {
    $temp = $array;
    unset($temp[$key]); // remove the current key

    // add our current value ($prefix.$value), and recursively call the function with
    // a new prefix.
    $ret = array_merge($ret, 
             array(trim($prefix.$value)), 
             (array) make_keyword_strings($temp, $newdepth, $prefix.$value.' '));
  }
  return $ret;
}

echo(implode("\n",make_keyword_strings($vals,3)));

---- Resulting output ----

2008
2008 Honda
2008 Honda Accord
2008 Honda Used
2008 Honda Car
2008 Accord
2008 Accord Honda
2008 Accord Used
2008 Accord Car
2008 Used
2008 Used Honda
2008 Used Accord
2008 Used Car
2008 Car
2008 Car Honda
2008 Car Accord
2008 Car Used
Honda
Honda 2008
Honda 2008 Accord
Honda 2008 Used
Honda 2008 Car
Honda Accord
Honda Accord 2008
Honda Accord Used
Honda Accord Car
Honda Used
Honda Used 2008
Honda Used Accord
Honda Used Car
Honda Car
Honda Car 2008
Honda Car Accord
Honda Car Used
Accord
Accord 2008
Accord 2008 Honda
Accord 2008 Used
Accord 2008 Car
Accord Honda
Accord Honda 2008
Accord Honda Used
Accord Honda Car
Accord Used
Accord Used 2008
Accord Used Honda
Accord Used Car
Accord Car
Accord Car 2008
Accord Car Honda
Accord Car Used
Used
Used 2008
Used 2008 Honda
Used 2008 Accord
Used 2008 Car
Used Honda
Used Honda 2008
Used Honda Accord
Used Honda Car
Used Accord
Used Accord 2008
Used Accord Honda
Used Accord Car
Used Car
Used Car 2008
Used Car Honda
Used Car Accord
Car
Car 2008
Car 2008 Honda
Car 2008 Accord
Car 2008 Used
Car Honda
Car Honda 2008
Car Honda Accord
Car Honda Used
Car Accord
Car Accord 2008
Car Accord Honda
Car Accord Used
Car Used
Car Used 2008
Car Used Honda
Car Used Accord


You can declarate a binary array X like { 0, 0, 0, 0, 1 }. And increment in binary logic. After every increment you print a generated string of this rule. If on array X word has 1, word i present, if 0 word not present.

You can use for it function:

string decbin  (  int $number  )

Her documentation you can find at: http://pl2.php.net/manual/en/function.decbin.php

Your algorithm stops when you reach array filled by the only ones. In decimal representation is:

pow(2, sizeof(X)) - 1


What you want to do is:

  1. Create an array L.
  2. For each word in the original text
    • If the word is not in L already, push the word to L.

Then it's a simple matter of generating all possible permutations of the type you require.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜