#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    42
    Rep Power
    12

    Group by explode value in foreach loop


    PHP Code:

        $res 
    ='<ul>';
        foreach( 
    $a['all'] as $b => $c ) {
        
    $d explode("|"$b);
        
    $res .='<li>'.$b[0].' '.$b[1].'</li>';
        }
        
    $res .='</ul>'
    result

    <ul>
    <li>John Monday</li>
    <li>Mary Monday</li>
    <li>Frank Monday</li>
    <li>John Thuesday</li>
    <li>Peter Thuesday</li>
    <li>Jack Thuesday</li>
    <li>Bob Friday</li>
    </ul>




    how can i get this
    group the result by day


    <ul id="Monday">
    <li>John</li>
    <li>Mary</li>
    <li>Frank</li>
    </ul>

    <ul id="Thuesday">
    <li>John</li>
    <li>Peter</li>
    <li>Jack</li>
    </ul>

    <ul id="Friday">
    <li>Bob</li>
    </ul>

    Thank you!
  2. #2
  3. No Profile Picture
    Super Moderator
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,399
    Rep Power
    24
    If your array/list is never really going to be a long item, such as several hundred/thousand items, then the following will be just fine. If it will be a long list, then the following will still work, but I do not suggest such since it runs through the entire list each time.

    At first, have an array that holds the 5 or 7 days of the week that you will be working with. Then start a while() loop where it takes the day step, then at end of each loop, you will step one day forward and check for the individuals defined on that day. Within each while() loop, hold a foreach() that withholds an if() statement to check if that individual is for that day, and echo if so.

    PHP Code:
    $rowcount = (Set via MySQL_num_rows() during your SELECT run);
    $members = (This array is built via your MySQL result exploded holding nested arrays);
    Visual example:
    $array = [
        [
    name0day0],
        [
    name1day1],
        [
    name2day2],
    ];
    $days = (Set the 5-7 days in this array);
    $res '';  // Only to initially define $res if not already. If is, remove this! 
    If your variables/arrays are all set to go, then lets build the list!

    PHP Code:
    while($i 0$i $rowcount$i++) {
        
    $res .= '<ul id="' $days[$i] . '">\n';
        foreach(
    $members as list($name$day)) {
            if(
    $day == $days[$i]) {
                
    $res .= '   <li>' $name '</li>\n';
            }
        }
        
    $res .= '</ul>\n';

    Last edited by Triple_Nothing; October 28th, 2014 at 10:29 AM.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    42
    Rep Power
    12
    Thank you !
  6. #4
  7. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,952
    Rep Power
    4037
    It's simpler to just group the results as you fetch them from the database (or whatever source).
    PHP Code:
    $members = array();
    while (
    $row=$query->fetch()){
        list(
    $name$day) = explode('|'$row['field']);
        
    $members[$day][] = $name;

    That will build the members array grouped by day, in the form of:
    Code:
    $members = array(
       [monday] => array('person 1', 'person 2', 'person 3')
       , [tuesday] => array('person 4', 'person 5')
       , [etc] => array('etc')
    )
    Then you just use a couple foreach loops to generate the output
    PHP Code:
    echo '<ul>';
    foreach (
    $members as $day=>$list){
       echo 
    '<li>'.$day.'<ul>';
       foreach (
    $list as $person){
          echo 
    '<li>'.$person.'</li>';
       }
       echo 
    '</ul></li>';
    }
    echo 
    '</ul>'

    Comments on this post

    • Triple_Nothing agrees : Excellent! This avoids multiple loops thru the nested arrays!
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    42
    Rep Power
    12
    Thanks that was helpful !
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,616
    Rep Power
    1766
    And just to be the token pedant: it's either Tuesday or Thursday, not Thuesday
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo