Page 1 of 1

arranging array

Posted: Mon Aug 15, 2005 12:09 pm
by shiznatix
ok heres the deal. i have an array of 500 phrases. i need to do somtin like this

take between 5 and 8 phrases wich are the shortest in characters. then for each one of those i need to do between 5 and 8 phrases then for each of those i need to do between 5 and 8 phrases and so on until im out of phrases. now i made it for the base phrases but i dont really know how to keep looping and adding on for the other levels of the array. im not really sure how to go about doing anything but making the base of it. here is what i have so far

Code: Select all

function make_lists($words)
{
    $temp = array_map('strlen',$words);
    asort($temp);
    $array = array();
    foreach($temp as $key => $val)
    {
      $array[$key] = $words[$key];
    }

    //$array is now the list of words sorted by length

    $num = rand(5, ;
    $num2 = $num*2;

    $num2 = ($num2 > count($array) ? count($array) : $num2);
    $arrnum = array();

    while (count($arrnum) < $num)
    {
        $rand = rand(1,2);
        if ($rand == 1)
        {
            $rand = rand(1, $num2);
            if (!in_array($rand, $arrnum))
                $arrnum[] = $rand;
        }
    }

    for($i=0; $i<count($arrnum); $i++)
    {
        $new[] = $array[$arrnum[$i]];
        unset($array[$arrnum[$i]]);
    }

    //$new is a array of the base words for the changed array
    //shuffle up the old array and reset the keys
    $array = ashuffle($array);

    //problems here -> loop till out of array elements or till you have between 5-8 links for the 1st page
 
    return $new;
}
any ideas would be supra

Posted: Mon Aug 15, 2005 12:19 pm
by feyd
I'm confused as to what you are wanting to do really..

Posted: Mon Aug 15, 2005 4:06 pm
by shiznatix
uhhh hummmm. ok i have an array that looks like this (but its 500 things big). remember that the array going in can be anywhere from 10 elements big to 500 elements big.

Code: Select all

$array = array(
'word one', 'word two', 'word three', 'word four'.......//etc
);
ok well that goes to five hundred. so in the first part of my function i get between 5 and 8 of those as my "base" and put them in a new array and unset them from the old array.

ok now lets say the new array looks like this right now after i set the base part.

Code: Select all

$new = array(
  'word five',
  'word two',
  'word three',
  'word seven',
  'word one',
  'word six'
);
now i need to start putting subarrays to each element (like tree brances?) so i need to make it look kinda like...

Code: Select all

$new = array(
  'word five' => array(
                                   'word seventeen',
                                   'word fourty three',
                                   'word eighty',
                                   'word ten',
                                   'word one hundred eight',
                                 ),
  'word two' => array(
                                  'word two hundred sixty eight',
                                  'word fourty',
                                  'word five hundred',
                                  'word thirteen',
                                  'word three hundred twenty two',
                                  'word two hundred sixteen',
                                  'word twenty five',
                                 ),
//same for the rest, notice that the length of the sub array can be between 5 and 8...randomly
);
then i need to do the same for each sub array element so i continue building on the array until i am out of elements of the original array. notice that there are no duplicate elements of the original array so each element after it gets put into the new array gets deleted from the original array so it wont be duplicated. so basically i am organizing the original array into a new array with sub arrays and everything.

does that make more sence?

Posted: Mon Aug 15, 2005 4:37 pm
by feyd
hmmm... I guess I should direct you to researching the concept of balanced trees..

Posted: Mon Aug 15, 2005 4:50 pm
by shiznatix
should, but obviously your drunk. so i understand but i just request that the next beer is mailed to me...unopened please.

maybe a sober person can give me some code or pseudo code, preferablly real code

Posted: Mon Aug 15, 2005 4:56 pm
by feyd
uh.. I'm not drunk, nor have I been drinking today, or yesterday.. or any day within the last month.

You can google for how balanced trees work..

Posted: Mon Aug 15, 2005 8:41 pm
by Ambush Commander
Er... I have no clue what you guys are talking about, but could this help?

http://en.wikipedia.org/wiki/Self-balan ... earch_tree

Posted: Tue Aug 16, 2005 4:13 am
by shiznatix
ok well i knew there would be no more than 600 words so i just did a tree like this

Code: Select all

foreach ($new as $key => $val)
    {
        if (count($array) > 
            $num = rand (5, ;
        else
            $num = (count($array));

        for ($i=0; $i<$num; $i++)
        {
            $new[$key][$array[$i]] = array();
            unset($array[$i]);
        }

        $array = ashuffle($array);

        if (count($array) < 1)
            break;
    }

    $lvl = 3;

    if (count($array) > 1)
    {
        foreach ($new as $key => $val)
        {
            foreach ($new[$key] as $key2 => $val2)
            {
                if (count($array) > 
                    $num = rand (5, ;
                else
                    $num = (count($array));

                for ($i=0; $i<$num; $i++)
                {
                    $new[$key][$key2][$array[$i]] = array();
                    unset($array[$i]);
                }

                $array = ashuffle($array);

                if (count($array) < 1)
                    break;
            }

            if (count($array) < 1)
                break;
        }
    }

    $lvl = 4;

    if (count($array) > 1)
    {
        foreach ($new as $key => $val)
        {
            foreach ($new[$key] as $key2 => $val2)
            {
                foreach ($new[$key][$key2] as $key3 => $val3)
                {
                    if (count($array) > 
                        $num = rand (5, ;
                    else
                        $num = (count($array));

                    for ($i=0; $i<$num; $i++)
                    {
                        $new[$key][$key2][$key3][$array[$i]] = array();
                        unset($array[$i]);
                    }

                    $array = ashuffle($array);

                    if (count($array) < 1)
                        break;
                }

                if (count($array) < 1)
                    break;
            }

            if (count($array) < 1)
                break;
        }
    }
it seams to work but its kinda slow, what you guys think, good or bad idea?