SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,648
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    combining two arrays?

    Heres a pre-test question im trying to wrap my head around,

    create a randomized array with values 1-11, where the first five elements and the last six elements are grouped together. Note: The order of the groups should also be randomized.

    But im lost on, heres what im thinking..
    PHP Code:
    $firstrange(15);
    $second range(611);
    shuffle($first); 
    shuffle($second);

    echo 
    join(', '$first)+" : "join(', '$second); 
    Am I on the right track?
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,648
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    this works..
    PHP Code:
    $firstrange(15);
    $second range(611);
    shuffle($first); 
    shuffle($second);
    echo 
    join(', '$first)." : ".join(', '$second); 
    but how do I randomize the order of the 2
    http://shores-rentals.com/luth/question7.php
    "Oh, and Jenkins--apparently your mother died this morning."

  3. #3
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,850
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Take a look at the PHP Manual on array functions:
    http://www.php.net//manual/en/ref.array.php

    After you have selected a suitable function, try this:
    PHP Code:

    <?php 
    echo '<pre>';
        
    $firstrange(15);
        
    $second range(611);
        
    shuffle($first); 
        
    shuffle($second);

        
    $third = array();
        
    // join(', ', $first)+" : "join(', ', $second);  
        
        
    print_r($first);
          echo 
    '<br />';

        
    print_r($second);
           echo 
    '<br />';

        
    print_r($third);
           echo 
    '<br />';

    echo 
    '</pre>';
    ?>
    Last edited by John_Betong; Jun 8, 2014 at 22:08. Reason: formatting
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  4. #4
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,648
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thanks for the hint, I used arr_merge()
    PHP Code:
        $firstrange(15);
        
    $second range(611);
        
    shuffle($first); 
        
    shuffle($second);

        
    $third = array();
        
    $third array_merge($first$second);
        
    // join(', ', $first)+" : "join(', ', $second);  
        
        
    print_r($first);
          echo 
    '<br />';

        
    print_r($second);
           echo 
    '<br />';

        
    print_r($third);
           echo 
    '<br />'
    But now how do I shuffle the two arrays inside the merged one?
    "Oh, and Jenkins--apparently your mother died this morning."

  5. #5
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,850
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Looks as though you never tried the code that I supplied and only looked at the link so...

    Try this and repeat for all arrays();
    PHP Code:
    echo '<pre>';

        echo 
    '<br />$first = range(1, 5);<br />'
            
    $firstrange(15);
            
    print_r($first);


        echo 
    '<br />shuffle($first); '
            
    shuffle($first);
            
    print_r($first);


    echo 
    '</pre>'
    Output:

    $first = range(1, 5);
    Array
    (
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    )

    shuffle($first); Array
    (
    [0] => 4
    [1] => 1
    [2] => 2
    [3] => 5
    [4] => 3 )
    Last edited by John_Betong; Jun 9, 2014 at 00:19. Reason: formatting
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  6. #6
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,648
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ok, heres the code
    PHP Code:
    echo '<pre>';
        echo 
    '<br />$first = range(1, 5);<br />';         
    $firstrange(15);        
    print_r($first);

        echo 
    '<br />shuffle($first); ';         
    shuffle($first);        
    print_r($first);

    echo 
    '</pre>';  
    echo 
    '<pre>';
        echo 
    '<br />$second = range(6, 11);<br />';        
     
    $secondrange(611);        
    print_r($second);

        echo 
    '<br />shuffle($second); ';         
    shuffle($second);       
     
    print_r($second);

    echo 
    '</pre>';  echo '<pre>';
            
    $thirdarray_merge($first,$second);        
    print_r($third);

        echo 
    '<br />shuffle($third); ';        
     
    shuffle($third);        
    print_r($third);

    echo 
    '</pre>'
    but after I shuffle the third array, all the values get shuffled and not just the 2 groups.

    Thanks for your help with this
    "Oh, and Jenkins--apparently your mother died this morning."

  7. #7
    SitePoint Evangelist N9ne's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you merge two arrays, it just adds all the elements into one array. You need to create a third array which holds the original two arrays. Then shuffle that which will randomise the order of those arrays. Then merge the two arrays that are inside that array.

  8. #8
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,648
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thats kind of where I get stuck, is this ok?
    PHP Code:
        $third = array([$first][$second]);
    shuffle($third);
    join(', '$third); 
    "Oh, and Jenkins--apparently your mother died this morning."

  9. #9
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,648
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
        $first range(15);
        
    $second range(611);
        
    shuffle($first); 
        
    shuffle($second);
    $third = array($first,$second);
    shuffle($third);
    echo 
    join(', ',$first);
    echo 
    '<br>';
    echo 
    join(', ',$second);
    echo 
    '<br>';
    echo 
    join(', ',$third); 
    produces
    5, 4, 2, 1, 3
    6, 11, 7, 8, 9, 10
    Array, Array
    "Oh, and Jenkins--apparently your mother died this morning."

  10. #10
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,648
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $third = array($first,$second);
    shuffle($third);
    array_merge($third);
    echo 
    join(', ',$first);
    echo 
    '<br>';
    echo 
    join(', ',$second);
    echo 
    '<pre>';
    print_r($third);
    echo 
    '</pre>'
    produces
    http://shores-rentals.com/luth/question7.php
    bot im trying to put it into a line
    "Oh, and Jenkins--apparently your mother died this morning."

  11. #11
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,648
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    think i got it,
    PHP Code:
    $first range(15);
    $second range(611);
    shuffle($first); 
    shuffle($second);
    $third = array($first,$second);
    shuffle($third);
    array_merge($third);
    echo 
    '<br><p>First Array:&nbsp;&nbsp;&nbsp;'.join(', ',$first).'</p>';
    echo 
    '<br><p>Second Array:&nbsp;&nbsp;&nbsp;'join(', ',$second) .'</p>';
    echo 
    '<br><p>Combined Array:&nbsp;&nbsp;&nbsp;'join(', ',$third[0]) .'&nbsp;&nbsp;:&nbsp;&nbsp;'join(', ',$third[1]) .'</p>'
    "Oh, and Jenkins--apparently your mother died this morning."

  12. #12
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,169
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lukeurtnowski View Post
    think i got it,
    PHP Code:
    $first range(15);
    $second range(611);
    shuffle($first); 
    shuffle($second);
    $third = array($first,$second);
    shuffle($third);
    array_merge($third);
    echo 
    '<br><p>First Array:***'.join(', ',$first).'</p>';
    echo 
    '<br><p>Second Array:***'join(', ',$second) .'</p>';
    echo 
    '<br><p>Combined Array:***'join(', ',$third[0]) .'**:**'join(', ',$third[1]) .'</p>'
    You can remove the call to array_merge, it isn't doing anything.

  13. #13
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,648
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    oh, thanks
    "Oh, and Jenkins--apparently your mother died this morning."


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •