Matching arrays

Discussion in 'PHP' started by jonathandey, Oct 11, 2010.

  1. #1
    Having some more array trouble =\
    I have this
    
    $a = explode(", ", $g->readby);
    $b = explode(", ", $g->view_date);
    
    		foreach($a as $keys => $values){
    			for($i=0;$i<count($b);$i++){
    				if($values == $user){
    					$value = array_values($b);
    					$thearray = array($values => $value);
    				}
    			}
    		}
    		
    		print_r($thearray);
    
    PHP:
    I have a structure in the database like this.
    User:
    
    hGWMr8, hGWMr8, hGWMr8, hGWMr8, hGWMr8, hGWMr8, hGWMr8, hGWMr8, hGWMr8, yt4QYj, hGWMr8
    
    Code (markup):
    Times:
    
    1286454350, 1286454350, 1286454350, 1286363232, 1286363232, 1286363232, 1286363232, 1286479111, 1286643445, 1286802447, 1286802447
    
    Code (markup):
    They are all adjacent to each other so the first in users is the timestamp for the first in times.

    What I'd like to be able to do is extract all the times specifically for each user, so if I want the times for hGWMr8 it'll give me 10 results rather than 11.

    If you think you need more information, please reply.
     
    jonathandey, Oct 11, 2010 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Below code should be helpful.

    
    $result = '';
    foreach($a as $key => $value)
    {
    	$result[$value][] = $b[$key];
    }
    
    PHP:
     
    mastermunj, Oct 11, 2010 IP
  3. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #3
    Hi, you should do this via sql query instead of using arrays:

    
    SELECT t1.User,t1.Times FROM TableName t1 NATURAL JOIN (SELECT DISTINCT User FROM TableName) t2;
    
    Code (markup):
    Check also GROUP_CONCAT function.
    Regards :)
     
    koko5, Oct 11, 2010 IP
  4. jonathandey

    jonathandey Active Member

    Messages:
    112
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    Thank you both for the reply, I have used the first answer though.
     
    jonathandey, Oct 12, 2010 IP