Current location: Hot Scripts Forums » Programming Languages » PHP » Remove value from array


Remove value from array

Reply
  #1 (permalink)  
Old 05-19-09, 08:04 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Remove value from array

Hi,

Would like to remove a value from an array. I have tried the following method but it doesn't work quite really well. Thus, I hope someone could improvise my method or suggest a totally new method to me.

This is the array that I currently have.

PHP Code:

// An array of Date > Weight > Name

Array
(
    [
2009-04-04] => Array
        (
            [
0.15] => Array
                (
                    [
0] => Choo
                
)

            [
1.05] => Array
                (
                    [
0] => Faiz
                    
[1] => Tan
                    
[2] => Murawala
                    
[3] => Modi
                    
[4] => Zou
                    
[5] => Hamid
                    
[6] => Ng
                    
[7] => Nur
                    
[8] => Ritik
                    
[9] => Cheang
                
)

            [
0.45] => Array
                (
                    [
0] => Quek
                
)

        )


)
// Another array with different date but same data
Array
(
    [
2009-04-05] => Array
        (
            [
0.15] => Array
                (
                    [
0] => Choo
                
)

            [
1.05] => Array
                (
                    [
0] => Faiz
                    
[1] => Tan
                    
[2] => Murawala
                    
[3] => Modi
                    
[4] => Zou
                    
[5] => Hamid
                    
[6] => Ng
                    
[7] => Nur
                    
[8] => Ritik
                    
[9] => Cheang
                
)

            [
0.45] => Array
                (
                    [
0] => Quek
                
)

        )



Base on the above, I have few dates but with same data on those dates. I want to randomly generate a name out from each date, however, considering to get the name from the highest weight first. Then remove the name from the array. Subsequently, after all the name from the highest weight gone, it will move to the next highest weight.
Though I have yet to work on the 'move to the next highest weight' thingy.

I have this current codes with me.

PHP Code:

foreach ($weekend as $WEEKENDS) {

    
// Set an array where each date of the weekend is assigned with a list of personnels.
    
$officerDateList = array($WEEKENDS => $kj);

      
// Get the Highest Weight Value
      
$max_key_val max(array_keys($officerDateList[$WEEKENDS]));

    
// Randomly select one personnel out from the array.
    
$inputSDOduty[] .= array_multi_rand($officerDateList[$WEEKENDS][$max_key_val]);
       
// After one personnel has been selected, we have to remove him from the list to prevent same name from being selected again.
    
$officerDateList[$WEEKENDS][$max_key_val] = array_diff($officerDateList[$WEEKENDS][$max_key_val], $inputSDOduty);
    
$officerDateList[$WEEKENDS][$max_key_val] = array_values($officerDateList[$WEEKENDS][$max_key_val]);

}


function 
array_multi_rand($array) {
    
srand((float) microtime() * 10000000);

    
$key array_rand($array);

    if (
is_array($array[$key])) {
        return 
array_multi_rand($array[$key]);        
    } else {
        return (
$array[$key]);
    }

Any suggestion? Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-20-09, 04:20 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Suggestion please? Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi-Dimensional Array Help Nikas PHP 17 05-04-09 03:10 AM
Add / Remove form elements, that post array m_abdelfattah JavaScript 7 06-24-07 10:11 PM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 04:54 PM
PHP - Unable to remove items from a file AdrianLewis PHP 5 03-17-06 06:14 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 09:14 PM


All times are GMT -5. The time now is 01:22 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.