This is what I have:
Code: Select all
$c = 'swiss cheese';
$value = array('$c', 'test2' );
echo $value . '<br/>';
echo("this is what is included $value");
Moderators: egami, macek, gesf
Code: Select all
$c = 'swiss cheese';
$value = array('$c', 'test2' );
echo $value . '<br/>';
echo("this is what is included $value");
Code: Select all
echo '<pre>';//to clean up output
print_r($value);
Code: Select all
$x=1;
$Edit_included = array( "null" ,$bread, $saladDressing, $tomato, $lettuce, $onion, );
echo 'Included: ';
foreach( $Edit_included as $new_details ){
echo $new_details;
echo', ';
$x++;
}
Code: Select all
$Edit_included = array( "null" ,$bread, $saladDressing, $tomato, $lettuce, $onion, );
$variable = 'Included: ';
foreach( $Edit_included as $new_details ){
$variable .= $new_details . ', ';
}
$variable = rtrim($variable,', ');
echo 'ta-da:<br />' . $variable;
It only outputs the last one in the array tho. I need all of it to be associated with $variable.seandisanti wrote:you don't want to assign a foreach loop to a variable (which would be done by having a function containing only the foreach loop that returns the value) you want to append values to a variable during a foreach loop.Code: Select all
$Edit_included = array( "null" ,$bread, $saladDressing, $tomato, $lettuce, $onion, ); $variable = 'Included: '; foreach( $Edit_included as $new_details ){ $variable .= $new_details . ', '; } $variable = rtrim($variable,', '); echo 'ta-da:<br />' . $variable;
Actually thats exactly what it is doing, it is appending the new value to the value that already exists rather than overwriting it. I'd rather just overwrite it. Maybe I did something wrong?seandisanti wrote:if it's only outputting the last, that tells me that you missed the '.' before the '='. So on each iteration it is overwriting the existing value instead of appending the new value. take another look at line 4 of my example
Code: Select all
$Edit_included = array( "null", $bread, $saladDressing, $tomato, $lettuce, $onion);
echo 'Included: ';
foreach( $Edit_included as $new_details ){
$variable .= $new_details . ', ';
}
$variable = rtrim($variable, ', ');
echo $variable;
No, you were partially correct. I do want to output the whole string, and what you provided was good. I just don't want the values appended/ added on the back of the last string.seandisanti wrote:It seems like i may have missed your intent.. I thought you wanted to output the whole list. if you only want to output the last, then remove the period before the equals in line 4.
This is what I'm getting (it doesn't seem to be appending any more, which is good): Included: null, , , tomato, lettuce, onion,seandisanti wrote:please show the output you're receiving, and the output you'd like.
Code: Select all
$Edit_included = array( "null", $bread, $saladDressing, $tomato, $lettuce, $onion);
echo 'Included: ';
foreach( $Edit_included as $new_details ){
$variable .= $new_details . ', ';
}
$variable = trim($variable, ',');
echo $variable;
Code: Select all
$Edit_included = array( "null", $bread, $saladDressing, $tomato, $lettuce, $onion);
echo 'Included: ';
foreach( $Edit_included as $new_details ){
$variable .= (!empty($new_details))?$new_details . ', ':'';
}
$variable = trim($variable, ',');
echo $variable;
PERFECT! thank you!!seandisanti wrote:try this:Code: Select all
$Edit_included = array( "null", $bread, $saladDressing, $tomato, $lettuce, $onion); echo 'Included: '; foreach( $Edit_included as $new_details ){ $variable .= (!empty($new_details))?$new_details . ', ':''; } $variable = trim($variable, ','); echo $variable;