+ Reply to Thread
Results 1 to 4 of 4

Thread: How to sort alternative keys and values from an associative array in PHP

  1. #1
    oceanwap's Avatar
    oceanwap is offline x10Hosting Member oceanwap is an unknown quantity at this point
    Join Date
    Nov 2009
    Location
    India
    Posts
    24

    How to sort alternative keys and values from an associative array in PHP

    I have an array in which I stores key and values using post method, so let's assume it looks like this
    PHP Code:
    $links = array("PHP" => "http://",
    "Zend" => "http://zend.com","Mysql" => "http://mysql.com",
    "Ubuntu" => "http://ubuntu.com"); 
    Now for fetching values I can do this
    PHP Code:

    foreach($links as $title => $link) {
    echo 
    "<tr><td><a href=\"$link\">$title</a></td></tr>"

    So I get the following html
    HTML Code:
    <tr><td><a href="http://php.net">PHP</a></td></tr>
    <tr><td><a href="http://zend.com">Zend</a></td></tr>
    <tr><td><a href="http://dev.mysql.com">Mysql</a></td></tr>
    <tr><td><a href="http://ubuntu.com">Ubuntu</a></td></tr> 
    But I want output like this, so document looks fancy
    HTML Code:
    <tr><td class="songbg"><a href="http://php.net">PHP</a></td></tr>
    <tr><td><a href="http://zend.com">Zend</a></td></tr>
    <tr><td class="songbg"><a href="http://dev.mysql.com">Mysql</a></td></tr>
    <tr><td><a href="http://ubuntu.com">Ubuntu</a></td></tr>
    How I can achieve this?

  2. #2
    dlukin is offline x10 Lieutenant dlukin is on a distinguished road
    Join Date
    Oct 2009
    Posts
    427

    Re: How to sort alternative keys and values from an associative array in PHP

    Code:
    $counter = 0 ;
    $tdclass = ''
    
    foreach($links as $title => $link) {
    
    $counter++ ;
    $tdclass = ($counter % 2 == 0 ) ? ' class="songbg" ' : '' ;
    
    echo "<tr><td$tdclass><a href=\"$link\">$title</a></td></tr>"; 
    
    }
    should work.
    Last edited by dlukin; 06-11-2010 at 01:06 PM.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: How to sort alternative keys and values from an associative array in PHP

    Slightly simpler, toggle the value in a variable.
    PHP Code:
    <?php $parity=0?>
    <ul>
        <?php foreach ($links as $title => $link) { ?>
            <li><a href="<?php echo $link ?>" class="<?php echo $parity 'odd' 'even' ?>"><?php echo $title?></a></li>
            <?php
            $parity 
    = !$parity
        } 
        
    ?>
    </ul>
    Note that I've also switch from a table to a list, since that's what the data is, structurally speaking. Don't abuse tables.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    oceanwap's Avatar
    oceanwap is offline x10Hosting Member oceanwap is an unknown quantity at this point
    Join Date
    Nov 2009
    Location
    India
    Posts
    24

    Re: How to sort alternative keys and values from an associative array in PHP

    Thanks it worked.

+ Reply to Thread

Similar Threads

  1. Replies: 4
    Last Post: 04-23-2010, 02:38 AM
  2. Passing values
    By liguehs in forum Programming Help
    Replies: 3
    Last Post: 07-27-2009, 03:05 PM
  3. Sort array alphabetically in PHP
    By ncusa367 in forum Scripts & 3rd Party Apps
    Replies: 3
    Last Post: 05-24-2008, 05:43 PM
  4. Recursive PHP array to Javascript array/object
    By Veridis in forum Programming Help
    Replies: 0
    Last Post: 04-16-2008, 02:55 AM
  5. [PHP] counting values in mysql_fetch_array()?
    By bonzo meier in forum Programming Help
    Replies: 4
    Last Post: 02-19-2008, 03:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers