.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   unsetting array's problem (PHP) (http://forums.bots-united.com/showthread.php?t=7607)

Ancient 16-05-2010 06:04

unsetting array's problem (PHP)
 
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;
                    }
            } 



All times are GMT +2. The time now is 08:12.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.