Jump to content

Problem with my first array implode


Shadowing

Recommended Posts

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

Link to comment
Share on other sites

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

?> 

 

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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

 

?>

Link to comment
Share on other sites

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

?> 

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.
?> 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

?> 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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

?> 

Link to comment
Share on other sites

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

?> 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

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.