MasterACE14 Posted May 29, 2011 Share Posted May 29, 2011 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 https://forums.phpfreaks.com/topic/237768-find-and-change-values-in-an-array/ Share on other sites More sharing options...
mikesta707 Posted May 29, 2011 Share Posted May 29, 2011 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 Link to comment https://forums.phpfreaks.com/topic/237768-find-and-change-values-in-an-array/#findComment-1221845 Share on other sites More sharing options...
MasterACE14 Posted May 29, 2011 Author Share Posted May 29, 2011 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 https://forums.phpfreaks.com/topic/237768-find-and-change-values-in-an-array/#findComment-1221847 Share on other sites More sharing options...
wildteen88 Posted May 29, 2011 Share Posted May 29, 2011 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); Link to comment https://forums.phpfreaks.com/topic/237768-find-and-change-values-in-an-array/#findComment-1221848 Share on other sites More sharing options...
Dathremar Posted May 29, 2011 Share Posted May 29, 2011 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); Link to comment https://forums.phpfreaks.com/topic/237768-find-and-change-values-in-an-array/#findComment-1221849 Share on other sites More sharing options...
mikesta707 Posted May 29, 2011 Share Posted May 29, 2011 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 https://forums.phpfreaks.com/topic/237768-find-and-change-values-in-an-array/#findComment-1221850 Share on other sites More sharing options...
MasterACE14 Posted May 29, 2011 Author Share Posted May 29, 2011 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 https://forums.phpfreaks.com/topic/237768-find-and-change-values-in-an-array/#findComment-1221851 Share on other sites More sharing options...
Dathremar Posted May 29, 2011 Share Posted May 29, 2011 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 https://forums.phpfreaks.com/topic/237768-find-and-change-values-in-an-array/#findComment-1221852 Share on other sites More sharing options...
MasterACE14 Posted May 29, 2011 Author Share Posted May 29, 2011 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 https://forums.phpfreaks.com/topic/237768-find-and-change-values-in-an-array/#findComment-1221856 Share on other sites More sharing options...
mikesta707 Posted May 29, 2011 Share Posted May 29, 2011 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 https://forums.phpfreaks.com/topic/237768-find-and-change-values-in-an-array/#findComment-1221858 Share on other sites More sharing options...
MasterACE14 Posted May 29, 2011 Author Share Posted May 29, 2011 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 https://forums.phpfreaks.com/topic/237768-find-and-change-values-in-an-array/#findComment-1221861 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.