Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP array comparison with different lengths
    primarykey
    data
    text
    <p>I need to compare values from 2 arrays that are not always the same size, each containing either a login or a logout time. I need to get the time difference total or time logged in between these values. Where I am getting caught is the logout result is usually smaller then the login. I tried the following but it isn't accurate at all.</p> <pre><code>$offset = 0; for($i=0;$i&lt;sizeof($login_ary);$i++){ //unset($time_logged); $time_logged = Array(); while( (int)$login_ary[$i] &gt; (int)$logout_ary[$i] ) { $offset++; } $time_logged['login'] = $login_ary[$i] + $offset; $time_logged['logout'] = $logout_ary[$i]; $calc_ary[] = $time_logged; } $time_logged_in = "undefined"; </code></pre> <p>End result I need to get either an array with lined up login/logoffs or a calculation of the total time difference between the login time and then logout time. But I have to exclude logins that don't have an associated logout.</p> <p><strong>EDITS</strong> </p> <p>1) Times inside of the arrays are unix timestamps 2) the arrays aren't static as this is done inside a loop for each user in a result set so the var_dump would look something like the following</p> <p>login_arr</p> <blockquote> <p>Array([0] => '1385402632',[1] => '1385763384',[2] => '1387293992') </p> </blockquote> <p>logout_arr</p> <blockquote> <p>Array([0] => '1387294012')</p> </blockquote> <p>** MY SOLUTION **</p> <p>My solution is out of context of the original question which is why I posted it as an edit instead of an answer. But here is what I did, the simplicity of it makes me feel foolish that I didn't think of it originally. What I did, is write the session timeout into the logout_ary since the data was going to be unrepresented otherwise, we decided that fudging a time was better then completely avoiding it. So I took the login time and fudged a logout time and spliced it into the array at the same point. I still would like to know the true answer to this problem if there is one though.</p> <pre><code> for($i=0;$i&lt;sizeof($login_ary);$i++){ //unset($time_logged); $time_logged = Array(); if( (int)$login_ary[$i] &gt; (int)$logout_ary[$i] ) { array_splice($logout_ary,$i,0,$login_ary[$i]+$session_length); $time_logged['login'] = $login_ary[$i]; $time_logged['logout'] = $logout_ary[$i]; } else { $time_logged['login'] = $login_ary[$i]; $time_logged['logout'] = $logout_ary[$i]; } $calc_ary[] = $time_logged; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload