Recommended Posts

Good Evening,

I'm having no luck in something that I'm sure is incredibly simply...

If I have an array like the following...

\$array = array(15,22,0,43,0,6);

How can I find and replace all the zeros with a one?

Regards, Ace

Share on other sites

you could loop through the array, and replace all zero's with a one like so

foreach(\$array as \$key=>\$value){
if (\$value==0)
\$array[\$key] = 1;
}//end foreach

Share on other sites

you could loop through the array, and replace all zero's with a one like so

foreach(\$array as \$key=>\$value){
if (\$value==0)
\$array[\$key] = 1;
}//end foreach

Thanks! I just worked that out before you posted.

I have another related problem. If I use...

\$values = array_count_values(\$array);
print_r(\$values);

I'm getting every value in the array except for the last one? any ideas why?

EDIT: Sorry stupid question. there's two 0's in the array, so I'm one less key/value.

Share on other sites

Or you can use array_map

function convertZero(\$value)
{
return (\$value === 0) ? 1 : \$value;
}

\$array = array(15,22,0,43,0,6);
\$array = array_map('convertZero', \$array);

Share on other sites

See http://www.php.net/manual/en/function.array-count-values.php for explanation of how array_count_values works.

As if You need the number of elements in the array just use count(\$array);

Share on other sites

you could loop through the array, and replace all zero's with a one like so

foreach(\$array as \$key=>\$value){
if (\$value==0)
\$array[\$key] = 1;
}//end foreach

Thanks! I just worked that out before you posted.

I have another related problem. If I use...

\$values = array_count_values(\$array);
print_r(\$values);

I'm getting every value in the array except for the last one? any ideas why?

Thanks!

can you post the actual array you are using, and the actual result. array_count_values() gives you a sort of frequency histogram (in array form) of the input array.

Share on other sites

Ultimately what I'm trying to do is, create a function that can take an array of X values and multiply them by F values(frequency of X). The problem with using array_count_values(); I have less key/values in the F array. Any ideas? Thanks!

Share on other sites

Ultimately what I'm trying to do is, create a function that can take an array of X values and multiply them by F values(frequency of X). The problem with using array_count_values(); I have less key/values in the F array. Any ideas? Thanks!

Share on other sites

Ultimately what I'm trying to do is, create a function that can take an array of X values and multiply them by F values(frequency of X). The problem with using array_count_values(); I have less key/values in the F array. Any ideas? Thanks!

ahhhh, completely wasn't seeing the key. *bangs head on desk* thanks heaps!

Cheers.

Share on other sites

Ultimately what I'm trying to do is, create a function that can take an array of X values and multiply them by F values(frequency of X). The problem with using array_count_values(); I have less key/values in the F array. Any ideas? Thanks!

As dathremar has said, you should probably read the manual entry for that function. Unless you have all unique values (in which case the frequency of each value would be 1) Then the number of values in F will always be smaller than the number of values in X. I'll give you an example.

\$array = array(1,2,3,4,5,1,2,4,5,6);
print_r(array_count_values(\$array));

the output of that code would be

 => 2
 => 2
 => 1
 => 2
 => 2
 => 1

Now, for what you ultimiately want to do, i'm a little confused. Do you want to take each value in X, and multiply it by its frequency? Or something else? if the former, you could do something like this

\$array = array(...);
\$frequency = array_count_values(\$array);

foreach(\$array as \$key=>\$value){
\$newValue = \$value * \$frequency[\$value];
\$array[\$key] = \$newValue;
}//end foreach

Hope this helps

Share on other sites

Now, for what you ultimiately want to do, i'm a little confused. Do you want to take each value in X, and multiply it by its frequency? Or something else? if the former, you could do something like this

\$array = array(...);
\$frequency = array_count_values(\$array);

foreach(\$array as \$key=>\$value){
\$newValue = \$value * \$frequency[\$value];
\$array[\$key] = \$newValue;
}//end foreach

Hope this helps

that's exactly what I'm trying to do, thanks a million Mike! appreciated.

Kind Regards, Ace

Archived

This topic is now archived and is closed to further replies.

×

• Activity

• Chat
×
• Create New...