Jump to content

Find and Change values in an array


MasterACE14

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

Link to comment
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.

 

Link to comment
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.

Link to comment
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!

Link to comment
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!

 

read my replay

Link to comment
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!

 

read my replay

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

 

Cheers.

Link to comment
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

[1] => 2
[2] => 2
[3] => 1
[4] => 2
[5] => 2
[6] => 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

Link to comment
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

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.