dc_jt Posted October 28, 2009 Share Posted October 28, 2009 Ive got address 1-5 fields listed on separate lines (they have to be like this) but I need to make sure there are no gaps in the lines. Therefore, if there is no address 1, this shows address 2, if there is no address 2 this shows address 3 etc. I cant work out the logic, I tried this but it doesnt work if there is no address 2 AND no address 3. Any ideas? if($add1 == ""){ $add1 = $add2; $add2 = $add3; $add3 = $add4; $add4 = $add5; $add5 = ''; } if($add2 == ""){ $add2 = $add3; $add3 = $add4; $add4 = $add5; $add5 = ''; } if($add3 == ""){ $add3 = $add4; $add4 = $add5; $add5 = ''; } if($add4 == ""){ $add4 = $add5; $add5 = ''; } Quote Link to comment https://forums.phpfreaks.com/topic/179355-need-help-with-a-bit-of-logic/ Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 $address = array($add1, $add2, $add3, $add4, $add5); $address = array_diff($address, array("")); echo implode("<br>",$address); Quote Link to comment https://forums.phpfreaks.com/topic/179355-need-help-with-a-bit-of-logic/#findComment-946311 Share on other sites More sharing options...
dc_jt Posted October 28, 2009 Author Share Posted October 28, 2009 Thanks for the quick reply. The thing is I cant just separate them with a line break. I am using placeholders on a pdf document so I need to set address 1 to a value, address 2 to a value etc so there is no gap. I.e Address 1 Address 4 Address 5 This is how it is coming out now because for example, address 2 and 3 are not set a value. It doesnt matter if address 5 doesnt have a value because it has moved upto address 5 becausse there will be nothing below that. Does that make sense? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/179355-need-help-with-a-bit-of-logic/#findComment-946320 Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 The last line was simply to show that the gaps are filled. array[0], array[1].... etc will each hold a line of the address, minus any blank lines. If only four lines have values, then there will only be elements up till array[3]. Use this however you like to fill in your PDF placeholders. $line1 = isset($array[0]) ? $array[0] : ""; $line2 = isset($array[1]) ? $array[1] : ""; $line3 = isset($array[2]) ? $array[2] : ""; ... etc etc Quote Link to comment https://forums.phpfreaks.com/topic/179355-need-help-with-a-bit-of-logic/#findComment-946322 Share on other sites More sharing options...
dc_jt Posted October 28, 2009 Author Share Posted October 28, 2009 Ok ill try that. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/179355-need-help-with-a-bit-of-logic/#findComment-946324 Share on other sites More sharing options...
dc_jt Posted October 28, 2009 Author Share Posted October 28, 2009 Nope still havent got it. This is my code: $add1 = "address1"; $add2 = "address2"; $add3 = "address3"; $add4 = "address4"; $add5 = "address5"; $address = array($add1, $add2, $add3, $add4, $add5); $array = array_diff($address, array("")); $add1 = isset($array[0]) ? $array[0] : ""; $add2 = isset($array[1]) ? $array[1] : ""; $add3 = isset($array[2]) ? $array[2] : ""; $add4 = isset($array[3]) ? $array[3] : ""; $add5 = isset($array[4]) ? $array[4] : ""; print_r($array); print("<br />");print("<br />"); print($add1); print("<br />"); print($add2); print("<br />"); print($add3); print("<br />"); print($add4); print("<br />"); print($add5); print("<br />"); Array ( [0] => address1 [3] => address3[4] => address4) address1 address3 address4 Quote Link to comment https://forums.phpfreaks.com/topic/179355-need-help-with-a-bit-of-logic/#findComment-946337 Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 oops. my fault. replace the line: $array = array_diff($address, array("")); with this: $array = array_values(array_diff($address, array(""))); Quote Link to comment https://forums.phpfreaks.com/topic/179355-need-help-with-a-bit-of-logic/#findComment-946343 Share on other sites More sharing options...
dc_jt Posted October 28, 2009 Author Share Posted October 28, 2009 Excellent!! Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/179355-need-help-with-a-bit-of-logic/#findComment-946350 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.