www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] implode array to avoid a loop

  1. #1
    Join Date
    Nov 2011
    Posts
    9

    resolved [RESOLVED] implode array to avoid a loop

    PHP Code:
    $groups $x->xpath("list/document/content[@name='group']");
    $groups array_unique($groups);

    if (!empty(
    $groups)){
    echo 
    '<ul class="groups">'.PHP_EOL;
    echo 
    '<li>'.implode('</li><li>''<a rel="category" href="page.php?tag='.urlencode($groups).'">'.$groups.'</a></li>').PHP_EOL;
    echo 
    '</ul>';


    this is what I'm trying to do, any help appreciated.

  2. #2
    Join Date
    Feb 2010
    Posts
    16
    I take it your code isn't working? What exactly are you trying to do and what's it doing?

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,222
    Maybe something like this?
    PHP Code:
    if (!empty($groups)){
        
    array_walk(
            
    $groups,
            
    create_function(
                
    '&$value, $key',
                
    '$value = "<li><a rel=\'category\' href=\'page.php?tag=".urlencode($value)."\'>".htmlspecialchars($value)."</a></li>";'
            
    )
        );
        echo 
    '<ul class="groups">'.PHP_EOL.implode(PHP_EOL$groups).PHP_EOL.'</ul>';

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Nov 2011
    Posts
    9
    That's exactly what I was trying to do NogDog. I read through the array functions last night trying to figure out how to do this without looping. I'm looping to parse the rest of the page, I just needed this menu first. This is perfect. Thank you for your help.

  5. #5
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233
    @NogDog question: Why use create_function instead of a true lambda? Note I get the same output with your code as with the following:

    PHP Code:
    if (!empty($groups)){
       
    array_walk(
          
    $groups,
          function(&
    $v$k) {
             
    $v "<li><a rel='category' href='page.php?tag=".urlencode($v)."'>".htmlspecialchars($v)."</a></li>";
          }
       );
       echo 
    '<ul class="groups">'.PHP_EOL.implode(PHP_EOL$groups).PHP_EOL.'</ul>';

    Just wondering if there is any reason to use one over the other? I personally like the inline lambda because then you don't have to escape the quotes in the string
    ~Ryan
    www.rdennispallas.com <-- Personal Site, changing regularly cuz its ugly.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,222
    Because not everyone is using PHP 5.3+ yet, unfortunately.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles