Jump to content

Problem with my first array implode


Shadowing

Recommended Posts

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

 

 

 

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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]}";    

}
?> 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'];

?> 

Link to comment
Share on other sites

  • 3 weeks later...

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) . ".";

}

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.