slpctrl Posted February 1, 2009 Share Posted February 1, 2009 Alright, I've been having a really really tough time trimming all the extra zeros at the end of an array that I've got. Here is what I'm trying to do: <?php for ($y=0; $y<10; $y++){ unset($pixls[$y]); } $num = array_sum($pixls); $num1 = (array_sum($pixls))-(10); for ($x=$num1; $x<$num; $x++){ unset($pixls[$x]); } ?> The first one works, the second doesn't and I can't for the life of me figure out why. Might anyone know why this code doesn't work? ??? Quote Link to comment https://forums.phpfreaks.com/topic/143318-solved-trimming-an-array/ Share on other sites More sharing options...
genericnumber1 Posted February 1, 2009 Share Posted February 1, 2009 array_sum() adds up all of the numbers inside of the array. I think you're looking for count() which returns the length of the array... although without thinking too much just dropping count() in for array_sum() how you're using it won't work, but I can't tell you how to fix it because I have no idea what you're trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/143318-solved-trimming-an-array/#findComment-751666 Share on other sites More sharing options...
slpctrl Posted February 1, 2009 Author Share Posted February 1, 2009 array_sum() adds up all of the numbers inside of the array. I think you're looking for count() which returns the length of the array... although without thinking too much just dropping count() in for array_sum() how you're using it won't work, but I can't tell you how to fix it because I have no idea what you're trying to do. Here is my code: <?php function getpixels($pic){ //Create a new image inside of the program to examine $image = imagecreatefrompng($pic); //get dimensions of said image list($width, $height) = getimagesize($pic); //Divides the height by 2 so that we're taking a 1 pixel slice from the MIDDLE $height = $height/2; //Creats an array called vars to store the digits representing the colors across $vars = array(); //Loops the program until it gets all the way to the end of the image for ($i=0; $i<=$width; $i++){ //Get's the color of the current position $color = imagecolorat($image, $i, $height); //uses array push to force the color of the current position to the end of the array array_push($vars, $color); } //Makes the function return the array return $vars; } $pixls = getpixels("barcode.png"); for ($y=0; $y<10; $y++){ unset($pixls[$y]); } $num = array_sum($pixls); $num1 = (count($pixls))-(10); for ($x=$num1; $x<$num; $x++){ unset($pixls[$x]); } foreach($pixls as $pixl){ echo $pixl; } $codes = array("0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011"); ?> (Don't worry about the $codes array, it's an array of barcode codes representing 1-9, but that'll come later when I can get the array that I'm working with properly stripped. There is a white border, represented by 0s. 10 on each side; I need to remove both from each side, and I for the life of me can't get it to work. Quote Link to comment https://forums.phpfreaks.com/topic/143318-solved-trimming-an-array/#findComment-751667 Share on other sites More sharing options...
printf Posted February 1, 2009 Share Posted February 1, 2009 replace this... $pixls = getpixels("barcode.png"); for ($y=0; $y<10; $y++){ unset($pixls[$y]); } $num = array_sum($pixls); $num1 = (count($pixls))-(10); for ($x=$num1; $x<$num; $x++){ unset($pixls[$x]); } with this... $pixls = array_slice ( getpixels ( 'barcode.png' ), 10, - 10 ); Quote Link to comment https://forums.phpfreaks.com/topic/143318-solved-trimming-an-array/#findComment-751669 Share on other sites More sharing options...
.josh Posted February 1, 2009 Share Posted February 1, 2009 okay wait wait wait...so is this still your barcode thing? And you're saying all those 1's and 0's are in an array for example, like this: array(0,0,0,0,1,0,1,1,0,0,1,0,1,0,0,0,0,0) and you're wanting to end up with this? array(1,0,1,1,0,0,1,0,1) See I thought you had them in a string, that's why I mentioned trim in some other thread (and why did you start a new thread for this?). If that's what the goal is, then do this: $string = array(0,0,0,0,1,0,1,1,0,0,1,0,1,0,0,0,0,0); $string = implode('',$string); $string = trim($string,'0'); $string = str_split($string); Quote Link to comment https://forums.phpfreaks.com/topic/143318-solved-trimming-an-array/#findComment-751670 Share on other sites More sharing options...
slpctrl Posted February 1, 2009 Author Share Posted February 1, 2009 replace this... $pixls = getpixels("barcode.png"); for ($y=0; $y<10; $y++){ unset($pixls[$y]); } $num = array_sum($pixls); $num1 = (count($pixls))-(10); for ($x=$num1; $x<$num; $x++){ unset($pixls[$x]); } with this... $pixls = array_slice ( getpixels ( 'barcode.png' ), 10, - 10 ); Thank you so much! It's weird though, sometimes there's the border (represented by 10 0s) is still there, sometimes it's not, and sometimes there's just one or 2 extra 0s on the end. But this is as close as I'm gonna get probably, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/143318-solved-trimming-an-array/#findComment-751673 Share on other sites More sharing options...
slpctrl Posted February 1, 2009 Author Share Posted February 1, 2009 okay wait wait wait...so is this still your barcode thing? And you're saying all those 1's and 0's are in an array for example, like this: array(0,0,0,0,1,0,1,1,0,0,1,0,1,0,0,0,0,0) and you're wanting to end up with this? array(1,0,1,1,0,0,1,0,1) See I thought you had them in a string, that's why I mentioned trim in some other thread (and why did you start a new thread for this?). If that's what the goal is, then do this: $string = array(0,0,0,0,1,0,1,1,0,0,1,0,1,0,0,0,0,0); $string = implode('',$string); $string = trim($string,'0'); $string = str_split($string); Perfect. Thank you so much. Quote Link to comment https://forums.phpfreaks.com/topic/143318-solved-trimming-an-array/#findComment-751675 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.