Current location: Hot Scripts Forums » Programming Languages » PHP » Filling Arrays


Filling Arrays

Reply
  #1 (permalink)  
Old 03-29-05, 06:50 AM
perleo perleo is offline
Coding Addict
 
Join Date: Jul 2003
Location: Ireland
Posts: 269
Thanks: 0
Thanked 0 Times in 0 Posts
Filling Arrays

I have the following code, i have declared the array times outside of the for loop,
inside the loop theres some stuff happening and it puts out one variable, which i want to add to the array

PHP Code:



$times 
= array();

 for(
$i=0;$i<=$num_row;$i++)
   {
         
#do some stuff
         
$var my_function();
 
         
$times array_fill($i1$var);
    }

foreach(
$times as $arrayval)
  {
      echo 
$arrayval;
      echo 
'</p>';
  } 
problem is it only puts in one value into the array, whats the correct way to do this to get it to work ?
Reply With Quote
  #2 (permalink)  
Old 03-29-05, 08:32 AM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
do it as simple as it be:
PHP Code:

$times[] = my_function(); 

code sample
PHP Code:

function my_function() {

    static 
$int=100;
    return --
$int;
}

for(
$i=0;$i<=$num_row;$i++) 
   { 
         
#do some stuff 
         
$times[] = my_function(); 
    } 

print_r($times); 
__________________
just an ignorant noob with moronic solution...
Reply With Quote
  #3 (permalink)  
Old 03-29-05, 09:11 AM
perleo perleo is offline
Coding Addict
 
Join Date: Jul 2003
Location: Ireland
Posts: 269
Thanks: 0
Thanked 0 Times in 0 Posts
here the actual code

PHP Code:

$row mysql_num_rows($q_hours);

      
      
$times = array();
      
          for(
$i=0;$i<=$row;$i++)
       {
          
$result mysql_fetch_array($q_hours);
          
          
$hour_depart $result['departure_time'];
          
$hour_arrive $result['arrival_time'];
          
          
#substract the hours
          
list($arrive_hour$arrive_min) = explode(":"$hour_arrive);
          list(
$depart_hour$depart_min) = explode(":"$hour_depart);
          
          
$mins $arrive_min $depart_min;
          
$hours $arrive_hour $depart_hour;
          
         
# echo $hours; echo ':'; echo $mins; echo '</p>';
          
          
$new_hour_string $hours.':'.$mins;
          
          
#now split that time and add padding
          
list($new_hour$new_min) = explode(":"$new_hour_string);
          
          if(
$new_hour <= '9')
           {
             
$Nhour '0'.$new_hour;
           }
           else
           {
             
$Nhour $new_hour;
           }
           
          if(
$new_min <= '9')
           {
             
$Nmin '0'.$new_min;
           }
           else
           {
             
$Nmin $new_min;
           }
          
           
#echo $Nhour.':'.$Nmin;
           #echo '</p>';
           
           
$NEW_HOUR $Nhour.':'.$Nmin;
           
           if(
$NEW_HOUR == '00:00')
            {
          
#do nothing
        
}
        else
        {
         
$times array_fill($i1$NEW_HOUR);
        }
           
       }
      
      foreach(
$times as $timearray)
       {
         echo 
$timearray;
     echo 
'</p>';
       } 
sorry, should of posted this first time, but how do i get it to add the $NEW_HOURS to the array all the time increasing the key?
so i get

[0] = sometime
[1] = anothertime
Reply With Quote
  #4 (permalink)  
Old 03-29-05, 09:52 AM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
simply do
PHP Code:

//before the loop

$j 0;

//inside the loop
//...deleted
  
$NEW_HOUR $Nhour.':'.$Nmin
            
           if(
$NEW_HOUR == '00:00'
            { 
          
#do nothing 
        

        else 
        { 
         
$times[$j] = $NEW_HOUR
         
$j++;
        } 
__________________
just an ignorant noob with moronic solution...
Reply With Quote
  #5 (permalink)  
Old 03-29-05, 10:02 AM
perleo perleo is offline
Coding Addict
 
Join Date: Jul 2003
Location: Ireland
Posts: 269
Thanks: 0
Thanked 0 Times in 0 Posts
perfect, thanks!
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
Can record arrays on database? mhs12grade1992 PHP 5 02-17-05 11:20 AM
arrays in functions Slypher PHP 3 01-30-05 05:36 AM
arrays DysignCore PHP 0 12-18-04 08:34 PM
How To Deal With Parenthesis' in Arrays? cebuy PHP 0 11-27-04 02:46 PM
Arrays cebuy PHP 1 10-25-04 07:40 AM


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