cornacum Posted March 6 Share Posted March 6 Hi everyone Can someone help me to re-write in a loop that works in PHP 8. I tried it myself but wasn't able to make it work. This is the code: if ( is_array( $u ) ) { while( list( $key ) = each( $u ) ) { $u = $u[$key]; break; } } Thanks Quote Link to comment Share on other sites More sharing options...
gw1500se Posted March 6 Share Posted March 6 Use foreach. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 6 Share Posted March 6 In this case, as it seems you want to replace the array with the value of the first element in the array, then $u = current($u); which would have been easier prior to v8 too. Quote Link to comment Share on other sites More sharing options...
cornacum Posted March 6 Author Share Posted March 6 1 hour ago, gw1500se said: Use foreach. Can you actually help me and rewrite this in foreach loop? I can't figure out what I am doing wrong. while(list($uid, $userdata) = each($user)): if ($userdata[2] == LEVEL_ADMIN) $role = 'ADMIN'; elseif ($userdata[2] == LEVEL_USER) $role = 'USER'; else $role = 'Unknown'; Quote Link to comment Share on other sites More sharing options...
Barand Posted March 6 Share Posted March 6 Did you use gw1500se's link to foreach() in the manual? There is no sign of in the code, so what did you try? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 6 Share Posted March 6 What does this array look like? Is it an array of arrays or just an array of values? Your two examples are confusing. Quote Link to comment Share on other sites More sharing options...
cornacum Posted March 6 Author Share Posted March 6 (edited) 1 hour ago, ginerjm said: What does this array look like? Is it an array of arrays or just an array of values? Your two examples are confusing. This is the loop within a function that populates the array at the bottom then returns it. $users[$uid] at the end. while ( strlen($userdata) > 72 ) { $u = unpack( 'H144', substr( $userdata, 0, 72) ); //$uid = hexdec( substr($u[1], 0, 4) ).' '; $u1 = hexdec( substr($u[1], 2, 2) ); $u2 = hexdec( substr($u[1], 4, 2) ); $uid = $u1+($u2*256); $cardno = hexdec( substr($u[1], 78, 2).substr($u[1], 76, 2).substr($u[1], 74, 2).substr($u[1], 72, 2) ).' '; $role = hexdec( substr($u[1], 4, 4) ).' '; $password = hex2bin( substr( $u[1], 8, 16 ) ).' '; $name = hex2bin( substr( $u[1], 24, 74 ) ). ' '; $userid = hex2bin( substr( $u[1], 98, 72) ).' '; //Clean up some messy characters from the user name $password = explode( chr(0), $password, 2 ); $password = $password[0]; $userid = explode( chr(0), $userid, 2); $userid = $userid[0]; $name = explode(chr(0), $name, 3); $name = utf8_encode($name[0]); $cardno = str_pad($cardno,11,'0',STR_PAD_LEFT); if ( $name == "" ) $name = $uid; $users[$uid] = array($userid, $name, $cardno, $uid,intval( $role ), $password); $userdata = substr( $userdata, 72 ); } return $users; And this is the loop fetching the array. while(list($uid, $userdata) = each($user)): if ($userdata[2] == LEVEL_ADMIN) $role = 'ADMIN'; elseif ($userdata[2] == LEVEL_USER) $role = 'USER'; else $role = 'Unknown'; Edited March 6 by cornacum Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 6 Share Posted March 6 (edited) foreach ($users as $key->$data) { list(($userid, $name, $cardno, $uid, $role, $password) = $data; if ($cardno == LEVEL_ADMIN) $role = 'ADMIN'; elseif($cardno == 'LEVEL_USER') $role = 'USER'; else $role = 'Unknown'; } Not sure what is supposed to be the result since you are doing the same thing for every array element. I suppose you might do an output as the last line of the loop such as: echo "$key is $role<br>"; Edited March 6 by ginerjm Used wrong var 'name' Quote Link to comment Share on other sites More sharing options...
cornacum Posted March 6 Author Share Posted March 6 I will give that a try. Thank you all for helping out. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 6 Share Posted March 6 oops I see that I type an extra left paren. Please fix. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.