[RESOLVED]Simple array_push not working as expected

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hyder_m29
Forum Newbie
Posts: 12
Joined: Mon Jun 23, 2008 9:36 am

[RESOLVED]Simple array_push not working as expected

Post by hyder_m29 »

$arr_hour = ("8.00/0", "8.00/0", "2.00/0", "8.00/0", "2.00/0", "0/0", "2.00/0", "0/0", "2.00/0") ;

Code: Select all

// variables declared here so accessible outside function scope
 
$arr_hour_regular = array();
$arr_hour_overtime = array();
    
    
    // goes through each hour item and splits it into overtime and regular, then pushes them into their resp. arrays
    function splitHours($hour_item, $hour_key, $delim) {
    
        $arr_hour_splitted = explode ($delim, $hour_item);
        array_push ($arr_hour_regular, $arr_hour_splitted[0]);
        //$arr_hour_overtime[] = $arr_hour_splitted[1];
    }
    
array_walk ($arr_hours, "splitHours", "/");
    
print_r ($arr_hour_regular);
OUTPUT:

Array ( )


I can print $arr_hour_splitted inside the function and it displays what it's supposed to,
so it does go through the array_walk function just fine. But why is it not pushing the
the strings into the new array?
Last edited by hyder_m29 on Mon Jun 23, 2008 7:56 pm, edited 2 times in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Simple array_push not working as expected

Post by Kieran Huggins »

das manual, ya wrote: Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
does that method produce the same problem?
hyder_m29
Forum Newbie
Posts: 12
Joined: Mon Jun 23, 2008 9:36 am

Re: Simple array_push not working as expected

Post by hyder_m29 »

Kieran Huggins wrote:
das manual, ya wrote: Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
does that method produce the same problem?
Yes it does, I'd read the exact same line while searching for a solution :D
hyder_m29
Forum Newbie
Posts: 12
Joined: Mon Jun 23, 2008 9:36 am

Re: Simple array_push not working as expected

Post by hyder_m29 »

** BUMP **

C'mon guys, it really shouldn't be so hard to figure out!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Simple array_push not working as expected

Post by Benjamin »

Your variable isn't available inside the function.
hitman6003
Forum Newbie
Posts: 4
Joined: Mon Jun 23, 2008 7:36 pm

Re: Simple array_push not working as expected

Post by hitman6003 »

As astions said, the $arr_hour_regular variable is not accessible from inside the function. This is called scope.

http://www.php.net/language.variables.scope
hyder_m29
Forum Newbie
Posts: 12
Joined: Mon Jun 23, 2008 9:36 am

Re: Simple array_push not working as expected

Post by hyder_m29 »

astions and hitman, thanks guys

that actually makes sense... for some reason, i thought the function was part of the container!
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: [RESOLVED]Simple array_push not working as expected

Post by Kieran Huggins »

DOH :banghead:
Good catch astions!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: [RESOLVED]Simple array_push not working as expected

Post by Benjamin »

Kieran Huggins wrote:Good catch astions!
Thanks Kieran, I'm pretty good at spotting errors.
Post Reply