Shadowing Posted January 11, 2012 Author Share Posted January 11, 2012 thanks Kevin for joining in. this is for a screen that builds buildings or units and im creating a script that displays what units was just built. I have a array of variables each variable is like the one below. <?php $field1 = "Farms to " . $_POST['farms_population']; $a = array_filter(array($field1, $field2, $field3, $field4, $field5, $field6, $field7)); ?> and im adding them to a SESSION <?php $_SESSION['mycache'] = "<h2>You have built</h2> " . implode(", ",$a) . "."; ?> and im wanting it to read You have built 4 farms, 5 barracks, 2 pools and 3 barns. so a "comma" after each one but a "and" on the last one Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/page/2/#findComment-1306605 Share on other sites More sharing options...
KevinM1 Posted January 11, 2012 Share Posted January 11, 2012 Part of the problem is that array_filter keeps the initial keys of the array, so it makes it hard to find the last element. You can't simply write something like: $a[count($a) - 1] = "and {$a[count($a) -1]}"; I suggest simply making a new array. Working example: $a = array("pigs", "horses", "cows", "chickens", null, "dogs", ""); $a = array_filter($a); $b = array(); foreach($a as $v) { $b[] = $v; } $b[count($b) - 1] = "and {$b[count($b) - 1]}"; echo implode(", ", $b); Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/page/2/#findComment-1306622 Share on other sites More sharing options...
Shadowing Posted January 11, 2012 Author Share Posted January 11, 2012 Thanks alot Kevin That fixed the issue where it would add "and" if every other variable was empty only problem now is if only one thing is built it says and 1 Farm so I added this and it fixed it. Everything works perfectly. if (count($b) > 1) { any chance i could trouble you to add a few comments for me please so i can learn im really confused on the "null" in the array and the double quotes at the end <?php $a = array($field1, $field2, $field3, $field4, $field5, $field6, null, $field7, ""); $a = array_filter($a); $b = array(); foreach($a as $v) { $b[] = $v; } if (count($b) > 1) { $b[count($b) - 1] = "and {$b[count($b) - 1]}"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/page/2/#findComment-1306639 Share on other sites More sharing options...
KevinM1 Posted January 11, 2012 Share Posted January 11, 2012 Ah, don't worry about those. They were used to ensure that array_filter successfully removed array values that would be considered false. Just test data on my end, so feel free to remove them. Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/page/2/#findComment-1306649 Share on other sites More sharing options...
Shadowing Posted January 11, 2012 Author Share Posted January 11, 2012 seriously thanks alot Kevin Ive never seen brackets used how you used them here "and {$b[count($b) - 1]}"; and I noticed my editor is showing them as if they were quotes in color wise Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/page/2/#findComment-1306658 Share on other sites More sharing options...
KevinM1 Posted January 11, 2012 Share Posted January 11, 2012 Brackets are necessary when you want to have the value of an array element be interpolated directly in a string. It basically says "Whatever is inside the brackets should be interpolated." I hate dropping in and out of a string to print an array value. To me, this: echo "Some db data: " . $row['id'] . " -- " . $row['title']; Looks ugly. echo "Some db data: {$row['id']} -- {$row['title']}"; Looks better, is simpler to write (fewer characters), and saves me from keeping track of all the .'s in my string. Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/page/2/#findComment-1306662 Share on other sites More sharing options...
jcbones Posted January 12, 2012 Share Posted January 12, 2012 My interpretation with the original code that I posted. <?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. } } //if the array still contains more than 2 indexes if(count($a) > 1) { $lastIndex = array_pop($a); //remove the last index and store it in a variable all its own. } $_SESSION['changed'] = "You have changed " . implode(", ",$a); //now implode will work properly. $_SESSION['changed'] .= (isset($lastIndex)) ? ' and ' . $lastIndex : NULL; //if the lastIndex variable exists, then append it, otherwise append nothing. echo $_SESSION['changed']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/page/2/#findComment-1306740 Share on other sites More sharing options...
Shadowing Posted January 28, 2012 Author Share Posted January 28, 2012 wierd lol i dont know why 3 weeks later I just now happend to notice the comma before the "and" How would I go about scratching that out. I havnt checked to see if Jcbones route does that too yet. You have built 5 Homes, 5 Naqahdah Mines, 5 Farms, and 5 Slave Camps. $a = array($field1, $field2, $field3, $field4, $field5); $a = array_filter($a); $b = array(); foreach($a as $v) { //foreach value in an array. $b[] = $v; } if (count($b) > 1) { // if only one variable exists then it wont add and $b[count($b) - 1] = "and {$b[count($b) - 1]}"; $_SESSION['mycache'] = "<h2>You have built</h2> " . implode(", ",$b) . "."; } Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/page/2/#findComment-1311914 Share on other sites More sharing options...
Shadowing Posted January 29, 2012 Author Share Posted January 29, 2012 Jcbones I got your version to work but I had to add a period like this in order to get the period on the end. $lastIndex = array_pop($a) . "."; but i also could just add the period in when i echo it lol. I tried for a few hours trying to get the comma out of your version of it Kevin. $field1 = "Farms to " . $_POST['farms_population']; $a = array($field1, $field2, $field3, $field4, $field5, $field6); foreach($a as $k => $v) { if(empty($v)) { unset($a[$k]); }} if(count($a) > 1) { $lastIndex = array_pop($a) . "."; } $_SESSION['changed'] = "You have changed " . implode(", ",$a); $_SESSION['changed'] .= (isset($lastIndex)) ? ' and ' . $lastIndex : NULL; echo $_SESSION['changed']; Quote Link to comment https://forums.phpfreaks.com/topic/254657-problem-with-my-first-array-implode/page/2/#findComment-1312130 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.