Current location: Hot Scripts Forums » Programming Languages » PHP » Multi-Dimensional Array Help


Multi-Dimensional Array Help

Reply
  #11 (permalink)  
Old 04-23-09, 02:12 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Ok. I think I have to change to another way. Is it possible to make another dimension in the array?

I have changed the function a little and add in other dates into the array. I have made a comment "// Newly Added".

PHP Code:

function unavailableDates(&$DB$personnelId$month$coy) {


        
// Base on the ID, check with the individual schedule
        
$queryID "SELECT individual_schedule_date FROM dms_individual_schedule WHERE personnel_id = $personnelId";
        
$rsID $DB->Execute($queryID);
        
$arrID $rsID->FetchRow();

        
// Remove the last 2 character (, ) from the string using substr
        // Use explode to convert string into array
        
$dates explode(', 'substr($arrID['individual_schedule_date'], 0, -2));
    
    
// Base on the Coy, check with the company schedule
       // Newly Added
    
$queryCoy "SELECT schedule_training_date FROM dms_company_schedule WHERE schedule_company = '$coy' AND schedule_training_date != ''";
    
$rsCoy $DB->Execute($queryCoy);
    
$arrCoy $rsCoy->FetchRow();
    
    
$datesCoy explode(', 'substr($arrCoy['schedule_training_date'], 0, -2));
    
        
// Get the available dates for the specified month instead of the whole schedule
        
$availableDates = array();
        foreach (
$dates as $dateValue) {
            if (
substr($dateValue52) == $month) {
                
$availableDates[] = $dateValue;
            }
        }
    
    
// Get the available dates for company for the specified month instead of the whole schedule
       // Newly added
        
$availableCoyDates = array();
        foreach (
$datesCoy as $dateValueCoy) {
            if (
substr($dateValueCoy52) == $month) {
                
$availableCoyDates[] = $dateValueCoy;
            }
        }
    
// Combine both the dates and place into one array
    
$availableDates_merge array_merge($availableDates$availableCoyDates);
    
        return 
$availableDates_merge;
    } 
Thus, currently, the result is

Code:
Array
(
    [Tan Ah Lian] => Array
        (
            [0] => 2009-04-20
            [1] => 2009-04-21
            [2] => 2009-04-22
            [3] => 2009-04-24
            [4] => 2009-04-25
        )

    [Joseph Gan] => Array
        (
            [0] => 2009-04-04
            [1] => 2009-04-05
            [2] => 2009-04-06
        )
)
Example, under the name "Tan Ah Lian". It's inclusive of two date that uses array_merge to combine the dates from two query.

Could I do it in a way that separate it? Like the below.

Code:
Array 
(
	[Tan Ah Lian] => Array
	    (
		[Individual Date] => Array
		    (
			[0] => 2009-04-20
			[1] => 2009-04-21
			[2] => 2009-04-22
		    )
		[Company Date] => Array
		    (
			[0] => 2009-04-24
			[1] => 2009-04-25
		    )
	    )
)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #12 (permalink)  
Old 04-23-09, 08:56 AM
forgueam's Avatar
forgueam forgueam is offline
iNET Code Serf
 
Join Date: Aug 2007
Location: West Chester, OH, USA
Posts: 37
Thanks: 0
Thanked 1 Time in 1 Post
Replace this line:

PHP Code:

$availableDates_merge array_merge($availableDates$availableCoyDates); 

With this:

PHP Code:

$availableDates_merge = array(

    
'Individual Date' => $availableDates,
    
'Company Date' => $availableCoyDates

Et Voila!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #13 (permalink)  
Old 04-27-09, 05:30 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Okie. Thanks. I've gotten that. I've this issue that I need to resolve. I'm thinking of doing it this way but unsure how to approach it.

Base on the current month and year, it will generate a array of dates of the current month.
Code:
Array
(
    [0] => 2009-04-01
    [1] => 2009-04-02
    [2] => 2009-04-03
    [3] => 2009-04-04
    [4] => 2009-04-05
    [5] => 2009-04-06
    [6] => 2009-04-07
    [7] => 2009-04-08
    [8] => 2009-04-09
    [9] => 2009-04-10
    [10] => 2009-04-11
    [11] => 2009-04-12
    [12] => 2009-04-13
    [13] => 2009-04-14
    [14] => 2009-04-15
    [15] => 2009-04-16
    [16] => 2009-04-17
    [17] => 2009-04-18
    [18] => 2009-04-19
    [19] => 2009-04-20
    [20] => 2009-04-21
    [21] => 2009-04-22
    [22] => 2009-04-23
    [23] => 2009-04-24
    [24] => 2009-04-25
    [25] => 2009-04-26
    [26] => 2009-04-27
    [27] => 2009-04-28
    [28] => 2009-04-29
    [29] => 2009-04-30
)
For each of the date, I need to allocate a name to it. Thus, I guess I need to loop through each of the date, and then executes codes to find out who will fit into the date. For example, from the previous codes you could see, I find a array of Name and Date. The dates are date that the Name are not free.

So how do you suggest me to approach this? Let me know if I'm not clear on the above.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #14 (permalink)  
Old 04-27-09, 07:31 AM
Jillian2 Jillian2 is offline
New Member
 
Join Date: Dec 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
You could do something like this to get the array indicies (personnel names):
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #15 (permalink)  
Old 04-27-09, 08:05 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
I don't get you. What you mean?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #16 (permalink)  
Old 04-28-09, 05:26 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Currently, I have 2 array. One normal and one multidimensional array. This is how it looks like.

Code:
// 1st Array
Array ( [0] => 2009-04-15 [1] => 2009-04-17 ) 

// 2nd Array
Array
(
    [ID] => 89
    [Name] => How are you
    [Company] => B
    [Schedule Date] => Array
        (
            [0] => 2009-04-28
            [1] => 2009-04-29
        )

)
How do I check if the date in Array2 exists in Array1?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #17 (permalink)  
Old 04-28-09, 05:38 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Ok. I think this way would solve it. I look at php.net for some examples.

PHP Code:

foreach ($new_array['Schedule Date'] as $shedule_date) {

        if ( 
in_array($shedule_date$gp) ) {
            echo 
"yes".$shedule_date.$new_array['Name'];
        }
        } 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #18 (permalink)  
Old 05-04-09, 03:10 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Okie. Met into some small problem. Would like to ask something.

I have this list.

Code:
// WarrantDateList
Array
(
    [2009-04-15] => Array
        (
            [0] => Name1
            [1] => Name1
            [2] => Name1
            [3] => Name1
            [4] => Name1
            [5] => Name1
        )

)

Array
(
    [2009-04-17] => Array
        (
            [0] => Name2
            [1] => Name2
            [2] => Name2
            [3] => Name2
            [4] => Name2
            [5] => Name2
        )

)
And what I wanted is to do a randomise selection of name in each array. I have the following code.

PHP Code:

function array_multi_rand($array) {

    
srand((float) microtime() * 10000000);
    
$Boo array_rand($array);
    if (
is_array($array[$Boo])) {
        return 
array_multi_rand($array[$Boo]);
    } else {
        return (
$array[$Boo]);
    }
}

// This is the code for me to randomise the array and get a name for each date.
// $gp = date
foreach ($gp as $GPS) {
    
$warrantDateList = array($GPS => $warrantList);
    
$inputGPduty[] .= array_multi_rand($warrantDateList);

Problem is that after I have done with array_multi_rand, it gives me this result.
Code:
// [0] suppose to be [2009-04-15] and [1] suppose to be [2009-04-17]
Array ( [0] => Name1 [1] => Name2 )
How can I do to the function so that the date remains as the key? Also, I would have to check if the values inside the array is the same. If it is, then I have to generate it again to get different value for each date.

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
[SOLVED] PHP Storing multi Dimensional array data in MySQL scott2500uk PHP 3 04-11-08 05:17 AM
[SOLVED] Sorting multi array on key patter PHP 8 03-27-08 04:23 PM
Two dimensional array Volkun C/C++ 2 08-29-07 07:35 AM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 04:54 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 09:14 PM


All times are GMT -5. The time now is 07:13 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.