1
$string = "The complete archive of The New York Times can now be searched from NYTimes.com " //the actual input is unknown, it would be read from textarea  

$size = the longest word length from the string 

I assigned and initialized array in for loop, for example array1, array2 ....arrayN, here is how i did

for ($i = 1; $i <= $size; $i++) {
    ${"array" . $i} = array();
}

so the $string would be divided in the length of the word

$array1 = [""];
$array2 = ["of", "be", ...]
$array3 = ["the", "can", "now", ...] and so on 

So, my question is how to assign in simple for loop or foreach loop $string value to $array1, $array2, $array3 ....., since the input text or the size of the longest word is unknown

2
  • I don't understand exactly what your asking - but you don't seem to understand how arrays work. Since you want an array where each element is text, simply do: $array[0] = "";, $array[1] = "of, be ..."; and so on (in your example, you have defined many arrays, but just used them as strings!) Feb 18, 2016 at 14:23
  • Did you give up or what??? Feb 24, 2016 at 15:47

4 Answers 4

3

I'd probably start with $words = explode(' ', $string) then sort the string by word length

usort($words, function($word1, $word2) {
    if (strlen($word1) == strlen($word2)) {
        return 0;
    }
    return (strlen($word1) < strlen($word2)) ? -1 : 1;
});

$longestWordSize = strlen(last($words));

Loop over the words and place in their respective buckets. Rather than separate variables for each length array, you should consider something like

$sortedWords = array(
    1 => array('a', 'I'),
    2 => array('to', 'be', 'or', 'is'),
    3 => array('not', 'the'),
);

by looping over the words you don't need to know the maximum word length.

The final solution is as simple as

foreach ($words as $word) {
    $wordLength = strlen($word);
    $sortedWords[ $wordLength ][] = $word;
}
1
  • Final solution looks oddly familiar. Feb 18, 2016 at 16:02
0

You could use something like this:

$words = explode(" ", $string);
foreach ($words as $w) {
    array_push(${"array" . strlen($w)}, $w);
}

This splits up $string into an array of $words and then evaluates each word for length and pushes that word to the appropriate array.

0
you can use explode().

$string = "The complete archive of The New York Times can now be searched           from NYTimes.com " ;

$arr=explode(" ",$string);
$count=count($arr);

$big=0;
 for ($i = 0; $i < $count; $i++) {
     $p=strlen($arr[$i]);
if($big<$p){ $big_val=$arr[$i]; $big=$p;}

     }
echo $big_val;
0

Just use the word length as the index and append [] each word:

foreach(explode(' ', $string) as $word) {
    $array[strlen($word)][] = $word;
}

To remove duplicates $array = array_map('array_unique', $array);.

Yields:

Array
(
    [3] => Array
        (
            [0] => The
            [2] => New
            [3] => can
            [4] => now
        )

    [8] => Array
        (
            [0] => complete
            [1] => searched
        )

    [7] => Array
        (
            [0] => archive
        )

    [2] => Array
        (
            [0] => of
            [1] => be
        )

    [4] => Array
        (
            [0] => York
        )

    [5] => Array
        (
            [0] => Times
        )
)

If you want to re-index the main array use array_values() and to re-index the subarrays use array_map() with array_values().

3
  • getting Undefined variable: array1 when i try to print $array1, $array2....and so on
    – Wow
    Feb 18, 2016 at 14:52
  • That's not how arrays work. Try something like: foreach($array as $len => $list) { echo "$len : " . implode(', ', $list) . "<br>\n"; } Feb 18, 2016 at 15:59
  • If you want words of length 3 then echo implode(', ', $array[3]); Feb 18, 2016 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.