setting values in associative array to 0 
July 17th, 2005, 01:29 PM
| | | 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 ? | 
July 17th, 2005, 01:29 PM
| | | Re: setting values in associative array to 0
try this out for starters:
foreach ($array as $key=>$value)
{ $key = 0; } | 
July 17th, 2005, 01:29 PM
| | | 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 | 
July 17th, 2005, 01:29 PM
| | | 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 :-( | 
July 17th, 2005, 01:29 PM
| | | 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 | 
July 17th, 2005, 01:29 PM
| | | 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 :-(( | 
July 17th, 2005, 01:29 PM
| | | 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 ? .? | 
July 17th, 2005, 01:29 PM
| | | 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 | 
July 17th, 2005, 01:29 PM
| | | 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 ) | 
July 17th, 2005, 01:29 PM
| | | 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/ | 
July 17th, 2005, 01:29 PM
| | | 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
================== | 
July 17th, 2005, 01:29 PM
| | | 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.
***************************** | 
July 17th, 2005, 01:30 PM
| | | 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 ... | 
July 17th, 2005, 01:30 PM
| | | 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/ | 
July 17th, 2005, 01:30 PM
| | | Re: setting values in associative array to 0
Well, just drop the whole array:
$array = array(). | 
July 17th, 2005, 01:30 PM
| | | 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 | 
July 17th, 2005, 01:30 PM
| | | 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/ | 
July 17th, 2005, 01:30 PM
| | | 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 | 
July 17th, 2005, 01:30 PM
| | | 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. | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | 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.
|