How do I order an array from a number in an array item?

Discussion in 'PHP' started by wwwbryan, Sep 5, 2010.

  1. #1
    I have the following code:

    
    			// check if the file exists
    			if (file_exists ('sys/data/online.txt'))
    			{
    				$read = file ('sys/data/online.txt');
    				
    				// read the online file
    				foreach ($read as $line)
    				{
    					$item = explode ("\t", $line);
    					
    					if (isset ($end_data))
    					{
    						$end_data .= $this-> disp_name ($item[0]) . '<br />';
    						
    					}
    					else
    					{
    						$end_data = $this-> disp_name ($item[0]) . '<br />';
    						
    					}
    					
    				}
    				if (isset ($end_data))
    				{
    					return $end_data;
    					
    				}
    			}
    
    PHP:
    It makes an array from a file by lines, then makes another array by separating each array item into another array with tabs. I want to order the names by the timestamp. How would I go about doing this. I have no idea how to even start this.
     
    wwwbryan, Sep 5, 2010 IP
  2. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #2
    would probably just order by dumping it into a db and letting that do it for me.

    if it's a pure linux timestamp though, can just use a bubblesort / any of the sorting algorithms to do it
     
    themullet, Sep 6, 2010 IP
  3. wwwbryan

    wwwbryan Well-Known Member

    Messages:
    181
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Ok I figured it out. I had to change my previous code to this:

    
    			// check if the file exists
    			if (file_exists ('sys/data/online.txt'))
    			{
    				$read = file ('sys/data/online.txt');
    				
    				// read the online file
    				foreach ($read as $line)
    				{
    					$item = explode ("\t", $line);
    					
    					if (!isset ($end_data))
    					{
    						$end_data = array();
    						
    					}
    						$end_data[$item[1]] = $this-> disp_name ($item[0]);
    						
    					
    				}
    				if (isset ($end_data))
    				{
    					ksort ($end_data);
    					
    					$end_data = array_reverse ($end_data);
    					
    					foreach ($end_data as $key => $disp_name)
    					{
    						if (isset ($result))
    						{
    							$result .= '<li>' . $disp_name . '</li>';
    							
    						}
    						else
    						{
    							$result = '<li>' . $disp_name . '</li>';
    						
    						}
    						
    					}
    					
    					return '<ul class="onlinelist">' . $result . '</ul>';
    					
    				}
    			}
    
    
    PHP:
     
    wwwbryan, Sep 6, 2010 IP