Connecting Tech Pros Worldwide Help | Site Map

setting values in associative array to 0

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 01:29 PM
pauld
Guest
 
Posts: n/a
Default setting values in associative array to 0

Ive tried almost every combination of while,foreach, key, value ,
array,0 I can some up with

Ive got an associative array that Ive been using as a counter
eg if (blah blah blah )
{$array['beans']++}
elseif (something else){$array['peas']++}
else {$array['carrots']++}


I then want to reset all the values to zero

foreach ($array as $key=>$value)
{ ??????? }
or array_walk ??

help please ?


  #2  
Old July 17th, 2005, 01:29 PM
mattvenables
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0

try this out for starters:

foreach ($array as $key=>$value)
{ $key = 0; }

  #3  
Old July 17th, 2005, 01:29 PM
Oli Filth
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0

mattvenables said the following on 06/06/2005 15:02:[color=blue]
> try this out for starters:
>
> foreach ($array as $key=>$value)
> { $key = 0; }
>[/color]

That should be $array[$key] = 0;

--
Oli
  #4  
Old July 17th, 2005, 01:29 PM
\(¯`·..Yttrium ...·´¯\)
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0


"pauld" <pdc124@yahoo.co.uk> a écrit dans le message de news:
1118065777.452178.115050@g49g2000cwa.googlegroups. com...[color=blue]
> Ive tried almost every combination of while,foreach, key, value ,
> array,0 I can some up with
>
> Ive got an associative array that Ive been using as a counter
> eg if (blah blah blah )
> {$array['beans']++}
> elseif (something else){$array['peas']++}
> else {$array['carrots']++}
>
>
> I then want to reset all the values to zero
>
> foreach ($array as $key=>$value)
> { ??????? }
> or array_walk ??
>
> help please ?
>[/color]


Hi,
I think the solution is :

foreach($array as $key =>$value)
{
$array[$key] = 0;
}

Best regards
Sorry for my english because I'm french :-(


  #5  
Old July 17th, 2005, 01:29 PM
pauld
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0

tried that but doesnt seemt to clear the array values.

IIRC there is soemthing that gives an array of keys
I could then do a $arrayofkeys as $k
{$array[$k]=0 }

cant find the function though

  #6  
Old July 17th, 2005, 01:29 PM
pauld
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0

reset($array);
foreach($array as $key =>$value)
{ $array[$key] = 0;}

didn't reset the array values to 0;

Im stuck :-((

  #7  
Old July 17th, 2005, 01:29 PM
\(¯`·..Yttrium ...·´¯\)
Guest
 
Posts: n/a
Default nd Re: setting values in associative array to 0


"pauld" <pdc124@yahoo.co.uk> a écrit dans le message de news:
1118071029.252822.297460@f14g2000cwb.googlegroups. com...[color=blue]
> reset($array);
> foreach($array as $key =>$value)
> { $array[$key] = 0;}
>
> didn't reset the array values to 0;
>
> Im stuck :-((
>[/color]

And

foreach($array as $key =>$value)
{ $array[$key] = "0";}


Does it ? .?


  #8  
Old July 17th, 2005, 01:29 PM
Ken Robinson
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0



pauld wrote:[color=blue]
> Ive tried almost every combination of while,foreach, key, value ,
> array,0 I can some up with
>
> Ive got an associative array that Ive been using as a counter
> eg if (blah blah blah )
> {$array['beans']++}
> elseif (something else){$array['peas']++}
> else {$array['carrots']++}
>[/color]

The following code has been tested and works:

<?
$tst = array('one' => 1, 'two'=>2, 'three' => 3);
echo '<pre>Before: ';print_r($tst); echo'</pre>';
foreach ($tst as $k=>$v)
$tst[$k] = 0;
echo '<pre>After: ';print_r($tst); echo'</pre>';
?>

Produces:

Before: Array
(
[one] => 1
[two] => 2
[three] => 3
)

After: Array
(
[one] => 0
[two] => 0
[three] => 0
)

Ken

  #9  
Old July 17th, 2005, 01:29 PM
Tony
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0

"pauld" <pdc124@yahoo.co.uk> wrote in message
news:1118071029.252822.297460@f14g2000cwb.googlegr oups.com...[color=blue]
> reset($array);
> foreach($array as $key =>$value)
> { $array[$key] = 0;}
>
> didn't reset the array values to 0;
>
> Im stuck :-((
>[/color]

Are you trying to reset the VALUES?

Say, you have an array: ( carrots=>3, tomatoes=>5, oranges=>6, apples=>2 ) -
you want all those values set to 0 -

try this:

foreach($array as $value) {
$value = 0;
}

Then you should have ( carrots=>0, tomatoes=>0, oranges=>0, apples=>0 )



  #10  
Old July 17th, 2005, 01:29 PM
Ewoud Dronkert
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0

On 6 Jun 2005 06:49:37 -0700, pauld wrote:[color=blue]
> help please ?[/color]

Tested and working:

$a = array( 'one' => 1, 'two' => 2, 'three' => 3 );

// solution 1
$b = $a;
foreach ( $b as $k => $v ) $b[$k] = 0;
print_r ( $b );

// solution 2 (PHP5)
$b = $a;
foreach ( $b as &$v ) $v = 0;
print_r ( $b );

// solution 3
function zero() { return 0; }
$b = array_map( 'zero', $a );
print_r ( $b );

// solution 4
$b = array_combine( array_keys( $a ), array_fill( 0, count( $a ), 0 ) );
print_r ( $b );


--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
  #11  
Old July 17th, 2005, 01:29 PM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0

pauld wrote:[color=blue]
> tried that but doesnt seemt to clear the array values.
>
> IIRC there is soemthing that gives an array of keys
> I could then do a $arrayofkeys as $k
> {$array[$k]=0 }
>
> cant find the function though
>[/color]
Maybe array_keys()?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
  #12  
Old July 17th, 2005, 01:29 PM
Chuck Anderson
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0

pauld wrote:
[color=blue]
>tried that but doesnt seemt to clear the array values.
>
>IIRC there is soemthing that gives an array of keys
>I could then do a $arrayofkeys as $k
>{$array[$k]=0 }
>
>cant find the function though
>
>
>[/color]
Not sure why the tested versions work, but this is from the manual:

"Note: Also note that foreach operates on a copy of the specified array
and not the array itself. Therefore, the array pointer is not modified
as with the each() construct, and changes to the array element returned
are not reflected in the original array. However, the internal pointer
of the original array is advanced with the processing of the array.
Assuming the foreach loop runs to completion, the array's internal
pointer will be at the end of the array."

And a possible solution:

"As of PHP 5, you can easily modify array's elements by preceding $value
with &. This will assign reference instead of copying the value. "


--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
  #13  
Old July 17th, 2005, 01:30 PM
Chung Leong
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0

For crying out loud, just use the stupid @ operator already. An
undefined entry will result in null, which gets typecast to 0.

@$array['beans']++ ;

@$array['carrots']++;

.... etc ...

  #14  
Old July 17th, 2005, 01:30 PM
Ewoud Dronkert
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0

On 7 Jun 2005 06:33:16 -0700, Chung Leong wrote:[color=blue]
> For crying out loud, just use the stupid @ operator already.
> @$array['beans']++ ;[/color]

I think his problem was not incrementing a certain element, but
resetting all elements to zero.


--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
  #15  
Old July 17th, 2005, 01:30 PM
Chung Leong
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0

Well, just drop the whole array:

$array = array().

  #16  
Old July 17th, 2005, 01:30 PM
R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0

Ewoud Dronkert wrote:[color=blue]
> On 6 Jun 2005 06:49:37 -0700, pauld wrote:[color=green]
> > help please ?[/color]
>
> Tested and working:
>
> $a = array( 'one' => 1, 'two' => 2, 'three' => 3 );
>
> // solution 1
> $b = $a;
> foreach ( $b as $k => $v ) $b[$k] = 0;[/color]
<snip>

IIRC, this is highly discouraged solution. Better use each().

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

  #17  
Old July 17th, 2005, 01:30 PM
Ewoud Dronkert
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0

On 8 Jun 2005 08:54:35 -0700, R. Rajesh Jeba Anbiah wrote:[color=blue][color=green]
>> foreach ( $b as $k => $v ) { ... }[/color]
>
> IIRC, this is highly discouraged solution. Better use each().[/color]

Per the manual, it is functionally identical to

reset( $b );
while ( list( $k, $v ) = each( $b ) ) { ... }

So certainly not "highly discouraged".


--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
  #18  
Old July 17th, 2005, 01:30 PM
R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0

Ewoud Dronkert wrote:[color=blue]
> On 8 Jun 2005 08:54:35 -0700, R. Rajesh Jeba Anbiah wrote:[color=green][color=darkred]
> >> foreach ( $b as $k => $v ) { ... }[/color]
> >
> > IIRC, this is highly discouraged solution. Better use each().[/color]
>
> Per the manual, it is functionally identical to
>
> reset( $b );
> while ( list( $k, $v ) = each( $b ) ) { ... }
>
> So certainly not "highly discouraged".[/color]

IMHO, you're quoting the manual wrongly. When you have to modify the
array, foreach is highly discouraged because of it's behavior (as
explained in manual).

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

  #19  
Old July 17th, 2005, 01:30 PM
Chung Leong
Guest
 
Posts: n/a
Default Re: setting values in associative array to 0

Inefficient? maybe. Highly discouraged? I don't think so.

Haven't benchmarked this, but array_walk() is probably the most
efficient way of modifying elements in an array, with the function
hopping from element to element using the internal linked list instead
of performing a hash lookup for each. Of course, the difference is
likely to be so small that it barely registers.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.