Shadowing Posted January 9, 2012 Share Posted January 9, 2012 I came accross a problem where im not sure how to add the word "and" inbetween my variables. I'm only displaying one $field below but i have 6 of them. If I add and as the seperator in the implode it will repeat it and and. Also it will put it at the end <?php $field1 = "Farms to " . $_POST['farms_population']; $a = array("$field1", "$field2", "$field3", "$field4", "$field5", "$field6"); $_SESSION['changed'] = "You have changed " . implode(" ",$a); echo $_SESSION['changed']; ?> wanting it to echo something like You have changed Farms to 30 and Larva Pools to 10 and Homes to 10 Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/ Share on other sites More sharing options...
Muddy_Funster Posted January 9, 2012 Share Posted January 9, 2012 could you put up a print_r($a) example for us? Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1305775 Share on other sites More sharing options...
Shadowing Posted January 9, 2012 Author Share Posted January 9, 2012 thanks for the responce Muddy_Funster Here you go <?php Array ( [0] => Farms to 30 [1] => [2] => Larva Pools to 10 [3] => [4] => Homes to 10 [5] => ) ?> Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1305778 Share on other sites More sharing options...
Muddy_Funster Posted January 9, 2012 Share Posted January 9, 2012 Give this a shot: <?php $field1 = "Farms to " . $_POST['farms_population']; $a = array("$field1", "$field2", "$field3", "$field4", "$field5", "$field6"); for ($i=0; $i < (count($a) -1); $i++){ $a[$i] = $a[$i]." and"; $_SESSION['changed'] = "You have changed " . implode(" ",$a); echo $_SESSION['changed']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1305790 Share on other sites More sharing options...
Shadowing Posted January 9, 2012 Author Share Posted January 9, 2012 ahh thanks alot i'll give this a try. Ive never used "FOR" before so this will be nice to use it somewhere i can relate to it Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1305796 Share on other sites More sharing options...
Shadowing Posted January 9, 2012 Author Share Posted January 9, 2012 Alright tested this out it works perfectly if all 6 $fields are used at once. but if I only use $field 1,2 and 3 it outputs this with double and's and one at the end what im doing is each $field is attached to a form which uses 6 inputs on one button. So each input that is enters it adds each variable but some times some inputs are not used which is why some times less then 6 $fields exist You have changed Farms to 30 and and Larva Pools to 5 and and Homes to 10 and Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1305809 Share on other sites More sharing options...
Muddy_Funster Posted January 9, 2012 Share Posted January 9, 2012 let's try a bit of conditioning then: <?php $field1 = "Farms to " . $_POST['farms_population']; $a = array("$field1", "$field2", "$field3", "$field4", "$field5", "$field6"); for ($i=0; $i < (count($a) -1); $i++){ if($a[$i] != ''){ $a[$i] = $a[$i]." and"; } $_SESSION['changed'] = "You have changed " . implode(" ",$a); echo $_SESSION['changed']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1305812 Share on other sites More sharing options...
KevinM1 Posted January 9, 2012 Share Posted January 9, 2012 Try: // array for-loop $imploded = substr(implode($a), 0, -4); // remove and_ from the end $_SESSION['changed'] = "You have changed $imploded"; Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1305814 Share on other sites More sharing options...
Shadowing Posted January 9, 2012 Author Share Posted January 9, 2012 geez you guys are smart This is what i have at the moment. It works accept i have a and at the end still. so i added what Kevin suggusted and it didnt wipe it out. It acctually made it repeat twice and not put correct spacing between the and's. so only problem now is the and at the end <?php You have changed Farms to 30 and Larva Pools to 3 and Homes to 10 and ?> <?php $a = array("$field1", "$field2", "$field3", "$field4", "$field5", "$field6"); for ($i=0; $i < (count($a) -1); $i++){ if($a[$i] != ''){ $a[$i] = $a[$i]." and"; }} $_SESSION['changed'] = "You have changed " . implode(" ",$a); echo $_SESSION['changed']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1305825 Share on other sites More sharing options...
Muddy_Funster Posted January 9, 2012 Share Posted January 9, 2012 can be a little cheeky and add this: <?php $field1 = "Farms to " . $_POST['farms_population']; $a = array("$field1", "$field2", "$field3", "$field4", "$field5", "$field6"); $a = array_unique($a); for ($i=0; $i < (count($a) -1); $i++){ if($a[$i] != ''){ $a[$i] = $a[$i]." and"; } $_SESSION['changed'] = "You have changed " . implode(" ",$a); echo $_SESSION['changed']; ?> scratch that, that's not going to help... Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1305839 Share on other sites More sharing options...
Muddy_Funster Posted January 9, 2012 Share Posted January 9, 2012 I think with this little tweek... <?php $field1 = "Farms to " . $_POST['farms_population']; $a = array("$field1", "$field2", "$field3", "$field4", "$field5", "$field6"); $a = array_unique($a); for ($i=0; $i < (count($a) -1); $i++){ if($a[$i+1] != ''){ $a[$i] = $a[$i]." and"; } $_SESSION['changed'] = "You have changed " . implode(" ",$a); echo $_SESSION['changed']; ?> how does that do? Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1305842 Share on other sites More sharing options...
Shadowing Posted January 9, 2012 Author Share Posted January 9, 2012 thats amazing it works now perfectly. I'm going to go look up the array_unique and read about it. it obviously keeps it from doubling. Trying to understand this code. any chance Muddy_Funster you could edit my comments on this so i can learn. im not sure what the -1 is doing and why does my code display not in php color <?php for ($i=0; $i < (count($a) -1); // while $i is less then how many $keys are in the array $i++){ // this making it keep adding by 1 if($a[$i] != ''){ // if a $key is not blank dont add a and? $a[$i] = $a[$i]." and"; // each $field turns into the $field plus with the and added. ?> Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1305847 Share on other sites More sharing options...
Muddy_Funster Posted January 9, 2012 Share Posted January 9, 2012 the php colour is an easy one, you are using code tags (the words "code" and "/code" writen within the square brackets) I'm using php tags (the words "php" and "/php" within the square brackets) now to the comments: <?php $field1 = "Farms to " . $_POST['farms_population']; $a = array("$field1", "$field2", "$field3", "$field4", "$field5", "$field6"); $a = array_unique($a); // removes all keys and values from array where the same VALUE is found, leaving only the first (lowest value Key) instance for ($i=0;// set the initial value of $i to 0 (zero) $i < (count($a) -1); // while $i is less then how many $keys are in the array - we use the -1 because the count() function starts at 1 and not 0 !!this check will be performed every run through the loop, and if it returns true the loop will run again, be carefull not to use a single = in here as you will get an infinate loop...!! $i++)// this making it keep adding by 1 (incramenting the value in $i) { if($a[$i+1] != '')// if the value in the NEXT $key along is not blank DO perform the following code block: { $a[$i] = $a[$i]." and";// each $field turns into the $field plus with the and added. (technical term: we concatonate the value in $a[$i] and the string value " and") } //not including an else statement implicitly tells PHP that there is nothing to do in the event that the if evaluates to false } $_SESSION['changed'] = "You have changed " . implode(" ",$a); echo $_SESSION['changed']; ?> if you need anything else let us know. Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1305856 Share on other sites More sharing options...
jcbones Posted January 9, 2012 Share Posted January 9, 2012 I would suggest a simpler concept, (unless I mis-understand the problem). <?php <?php $field1 = "Farms to " . $_POST['farms_population']; $a = array($field1, $field2, $field3, $field4, $field5, $field6); //no need to quote variables. foreach($a as $k => $v) { //foreach value in an array. if(empty($v)) { //if the value is empty (you will not need it, because it doesn't hold a value). unset($a[$k]); //so get rid of it. } } $_SESSION['changed'] = "You have changed " . implode(" and ",$a); //now implode will work properly. echo $_SESSION['changed']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1305876 Share on other sites More sharing options...
KevinM1 Posted January 9, 2012 Share Posted January 9, 2012 so i added what Kevin suggusted and it didnt wipe it out. It acctually made it repeat twice and not put correct spacing between the and's. so only problem now is the and at the end Works fine for me: $a = array("pigs", "horses", "cows", "chickens"); foreach($a as $k => $v) { $a[$k] = "$v and "; } $i = substr(implode($a), 0, -4); echo $i; pigs and horses and cows and chickens That said, jcbones' solution is better. Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1305882 Share on other sites More sharing options...
Shadowing Posted January 9, 2012 Author Share Posted January 9, 2012 idk maybe i did something wrong when i added in what you said Kevin but yah i just tried jcbones solution and it works perfectly thanks a ton for that jcbones and its much easier for a noob like me to understand. specially since you gave me notes with it too. thanks Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1305885 Share on other sites More sharing options...
Shadowing Posted January 10, 2012 Author Share Posted January 10, 2012 To make things more complicated lol how would one go about doing commas after each one but adding and for the last one. <?php $a = array($field1, $field2, $field3, $field4, $field5, $field6); foreach($a as $k => $v) { if(empty($v)) { unset($a[$k]); } } $_SESSION['mycache'] = "<h2>You have built</h2> " . implode(", ",$a); ?> Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1306145 Share on other sites More sharing options...
Muddy_Funster Posted January 10, 2012 Share Posted January 10, 2012 not tested, but I think this could do it (seems logical to me at any rate, but that seldom counts for much...): <?php $a = array($field1, $field2, $field3, $field4, $field5, $field6); foreach($a as $k => $v) { if(empty($v)) { unset($a[$k]); } if(!$a[$k+1]){ //if there isn't another record after this one: $a[$k-1] .= ' And '.$v; // the record before this one has ' And '.$v ($v the value of this record) appended to it unset($a[$k]); // this record is then dropped as it's value has been appended to the previous one } } $_SESSION['mycache'] = "<h2>You have built</h2> " . implode(", ",$a); ?> I'm just not sure how it's going to handle any empty values at the end of the array. It may be that you need to concatenate the last two array values after the loop, but before the explode... Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1306158 Share on other sites More sharing options...
Shadowing Posted January 10, 2012 Author Share Posted January 10, 2012 I was reading about array filters and inserting into arrays. isnt there like a way to like filter the array for the last one then insert and into it. lol idk I got this for the output And 1 Home, And , And , And 2 Farms, And , And , And 3 Worship Shrines, And , And , And 4 Larva Pools, And , And , And , And , And , And maybe count the total in the array and subtract one so the comma isnt there then count the total again and insert and is the largest problem getting rid of the last comma? Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1306253 Share on other sites More sharing options...
Shadowing Posted January 11, 2012 Author Share Posted January 11, 2012 oh boy im getting closer to figuring this out I think. so far im able to figure out the max key number that isnt blank using $max_key = max(array_keys(array_filter(array($field1, $field2, $field3, $field4, $field5, $field6, $field7)))); so now i need to figure out how to insert and subtract the comma on the key that equals $max_key <?php $a = array($field1, $field2, $field3, $field4, $field5, $field6, $field7); foreach($a as $k => $v) { //foreach value in an array. if(empty($v)) { //if the value is empty (you will not need it, because it doesn't hold a value). unset($a[$k]); //so get rid of it. max_key= max(array_keys(array_filter(array($field1, $field2, $field3, $field4, $field5, $field6, $field7)))); ?> Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1306539 Share on other sites More sharing options...
Shadowing Posted January 11, 2012 Author Share Posted January 11, 2012 oh wow i just figured out a really simple way to add a comma between everything. just apply the array_filter from the start. <?php $a = (array_filter(array($field1, $field2, $field3, $field4, $field5, $field6, $field7))); $_SESSION['mycache'] = "<h2>You have built</h2> " . implode(", ",$a) . "."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1306551 Share on other sites More sharing options...
Shadowing Posted January 11, 2012 Author Share Posted January 11, 2012 omg i almost figured it out <?php $a = array_filter(array($field1, $field2, $field3, $field4, $field5, $field6, $field7)); foreach($a as $k => $v) { if(!$a[$k+1]){ $a[$k-1] .= ' And '.$v; unset($a[$k]); $_SESSION['mycache'] = "<h2>You have built</h2> " . implode(", ",$a) . "."; } } ?> The return on this is You have built 3 soldiers, 4 Cannons, 2 Agents, 5 Ships, 2 pools, 5 Jets And 2 Boats. The only problem is now. is that if I only build one thing it shows up with "and" at the begining. not sure how to wipe that out You have built and 3 soldiers. Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1306565 Share on other sites More sharing options...
Shadowing Posted January 11, 2012 Author Share Posted January 11, 2012 Yes!!!! Problem solved. i dont know if this is the shortest form of code to do this in <?php $a = array_filter(array($field1, $field2, $field3, $field4, $field5, $field6, $field7)); if (count($a) > 1) { foreach($a as $k => $v) { if(!$a[$k+1]){ $a[$k-1] .= ' And '.$v; unset($a[$k]); $_SESSION['mycache'] = "<h2>You have built</h2> " . implode(", ",$a) . "."; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1306571 Share on other sites More sharing options...
Shadowing Posted January 11, 2012 Author Share Posted January 11, 2012 bleh i found a flaw in this If every other $field is blank then the return is. Maybe the if count is doing this and 1 Cannon, and 2 Ships, and Bombers. Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1306589 Share on other sites More sharing options...
KevinM1 Posted January 11, 2012 Share Posted January 11, 2012 Stop. Just stop. You're throwing a bunch of code at the wall hoping that something will stick. What is it you're trying to do? What's your data, and what is the desired result? Because right now you've over-engineered whatever you want to do to the point of gibberish. Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/#findComment-1306593 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.