Jump to content

'each' function deprecated


cornacum

Recommended Posts

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';

 

Link to comment
Share on other sites

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 by cornacum
Link to comment
Share on other sites

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 by ginerjm
Used wrong var 'name'
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.