473,386 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 ?

Jul 17 '05 #1
18 1837
try this out for starters:

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

Jul 17 '05 #2
mattvenables said the following on 06/06/2005 15:02:
try this out for starters:

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


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

--
Oli
Jul 17 '05 #3

"pauld" <pd****@yahoo.co.uk> a écrit dans le message de news:
11**********************@g49g2000cwa.googlegroups. com...
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 ?

Hi,
I think the solution is :

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

Best regards
Sorry for my english because I'm french :-(
Jul 17 '05 #4
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

Jul 17 '05 #5
reset($array);
foreach($array as $key =>$value)
{ $array[$key] = 0;}

didn't reset the array values to 0;

Im stuck :-((

Jul 17 '05 #6

"pauld" <pd****@yahoo.co.uk> a écrit dans le message de news:
11**********************@f14g2000cwb.googlegroups. com...
reset($array);
foreach($array as $key =>$value)
{ $array[$key] = 0;}

didn't reset the array values to 0;

Im stuck :-((


And

foreach($array as $key =>$value)
{ $array[$key] = "0";}
Does it ? .?
Jul 17 '05 #7


pauld wrote:
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']++}


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

Jul 17 '05 #8
"pauld" <pd****@yahoo.co.uk> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
reset($array);
foreach($array as $key =>$value)
{ $array[$key] = 0;}

didn't reset the array values to 0;

Im stuck :-((


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 )

Jul 17 '05 #9
On 6 Jun 2005 06:49:37 -0700, pauld wrote:
help please ?


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/
Jul 17 '05 #10
pauld wrote:
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

Maybe array_keys()?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #11
pauld wrote:
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

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.
*****************************
Jul 17 '05 #12
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 ...

Jul 17 '05 #13
On 7 Jun 2005 06:33:16 -0700, Chung Leong wrote:
For crying out loud, just use the stupid @ operator already.
@$array['beans']++ ;


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/
Jul 17 '05 #14
Well, just drop the whole array:

$array = array().

Jul 17 '05 #15
Ewoud Dronkert wrote:
On 6 Jun 2005 06:49:37 -0700, pauld wrote:
help please ?


Tested and working:

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

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

<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

Jul 17 '05 #16
On 8 Jun 2005 08:54:35 -0700, R. Rajesh Jeba Anbiah wrote:
foreach ( $b as $k => $v ) { ... }


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


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/
Jul 17 '05 #17
Ewoud Dronkert wrote:
On 8 Jun 2005 08:54:35 -0700, R. Rajesh Jeba Anbiah wrote:
foreach ( $b as $k => $v ) { ... }


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


Per the manual, it is functionally identical to

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

So certainly not "highly discouraged".


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

Jul 17 '05 #18
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.

Jul 17 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stefan Richter | last post by:
Hi, I want to create an associative Array with a PHP variable (article ID) as Key and another associative array as it's value. How do I instanciate it, how can I fill it? I want something...
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
5
by: soup_or_power | last post by:
Hi I have an associative array like this: arr=30; arr=20;arr=40;arr=10; I want the sort function to sort keys in ascending order of the values on the right hand side with the following result:...
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.