View Single Post
unsetting array's problem (PHP)
Old
  (#1)
Ancient
PodBot MM's Laziest Waypointer
 
Ancient's Avatar
 
Status: Offline
Posts: 1,010
Join Date: Jan 2005
Location: Nebraska, United States of America
Default unsetting array's problem (PHP) - 16-05-2010

I've hit a bump in the road.

Current Arrays:

$find_user
PHP Code:
Array([0] => Array([0] => 'Ancient', [username] => 'Ancient', [1] => 'Password', [password] => 'password'),
[
1] => Array([0] => 'Ancient2', [username] => 'Ancient2', [1] => 'Password', [password] => 'Password')) 
Right now I'm trying to get rid of [0] => 'Ancient', and just leave [username] => and [password].
The goal is to unset some array #s and keep the array keys.

Goal:
PHP Code:
Array([0] => Array([username] => Ancient, [password] => Password),
[
1] => Array([username] => Ancient2, [password] => Password)) 
What I have now:
PHP Code:
function kill_num_array($item$array false)
            {
                if(!
$array)
                    {
                        
//
                        // Gets rid of numbered arrays.
                        // Example: $find_user[0]['id'] changes to $find_user['id']
                        //
                        
$i 0;
                        while(
count($item[0]) >= $i)
                            {
                                unset(
$item[0][$i]); // Error Log shows this line is bad.
                                
$i++;
                            }
                        unset(
$i);
                        return 
$item[0];
                    }
                else{
                        
//
                        // Gets rid of sub-numbered arrays
                        // Example: $find_user[1][0]  changes to $find_user[1]['some_key']
                        //
                        
$i 0;
                        
$a 0;
                        while(
count($item[$a]) >= $i)
                            {
                                unset(
$item[$a][$i]);
                                
$i++;
                                if(
$i count($item[$a]))
                                    {
                                        
$a++;
                                        
$i 0;
                                    }
                                if(
$a count($item))
                                    {
                                        unset(
$a$i);
                                        exit;
                                    }
                            }
                        continue;
                        return 
$item;
                    }
            } 
To me the function looks fine.
But PHP must really hate me because it's throwing up a bunch of errors now.
What's weird, is that this worked before and now even though I haven't touched it, it's throwing up errors.


EDIT:
After a long time of debugging I finally found the answer.
PHP Code:
function kill_num_array($item$array false)
            {
                if(!
$array)
                    {
                        
//
                        // Gets rid of numbered arrays.
                        // Example: array($find_user[0], $find_user['id']) changes to $find_user['id']
                        //
                        
$i 0;
                        while(
count($item) >= $i)
                            {
                                unset(
$item[$i]);
                                
$i++;
                            }
                        unset(
$i);
                        return 
$item;
                    }
                else{
                        
//
                        // Gets rid of sub-numbered arrays
                        // Example: $find_user[1][0]  changes to $find_user[1]['some_key']
                        //
                        
$i 0;
                        
$a 0;
                        while(
count($item[$a]) >= $i)
                            {
                                unset(
$item[$a][$i]);
                                
$i++;
                                if(
$i >= count($item[$a]))
                                    {
                                        
$a++;
                                        
$i 0;
                                    }
                                if(
$a >= count($item))
                                    {
                                        unset(
$a$i);
                                        exit;
                                    }
                            }
                        continue;
                        return 
$item;
                    }
            } 


[Web Designer][Waypointer][Gamer]
CFE Games Administrator
[CFE]Games.com
[Never Trust the Untrusted]

Last edited by Ancient; 16-05-2010 at 07:35..
  
Reply With Quote