Array issue

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

sweetpeet
New php-forum User
New php-forum User
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Array issue

Postby sweetpeet » Mon Apr 01, 2013 7:32 am

I am having a problem with creating this array.
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");


For some reason when it runs it just outputs the word "Array". Does anyone know why?

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Array issue

Postby seandisanti » Mon Apr 01, 2013 8:44 am

because arrays aren't strings. even when they're full of strings, they're still arrays. there are a few diff ways to view the contents, but the easiest is probably just:

Code: Select all

echo '<pre>';//to clean up output
print_r($value);

sweetpeet
New php-forum User
New php-forum User
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: Array issue

Postby sweetpeet » Mon Apr 01, 2013 10:07 am

Thanks for the help, I decided to do something else that worked.

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++;
   }


However, now that I have that(above). Is it possible to assign a foreach loop to a variable?

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Array issue

Postby seandisanti » Mon Apr 01, 2013 10:14 am

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;
 

sweetpeet
New php-forum User
New php-forum User
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: Array issue

Postby sweetpeet » Mon Apr 01, 2013 10:29 am

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;


It only outputs the last one in the array tho. I need all of it to be associated with $variable.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Array issue

Postby seandisanti » Mon Apr 01, 2013 10:31 am

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

sweetpeet
New php-forum User
New php-forum User
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: Array issue

Postby sweetpeet » Mon Apr 01, 2013 10:53 am

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


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?

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;



It also leaves commas when the variable before is unset which is a problem.
Last edited by sweetpeet on Mon Apr 01, 2013 10:57 am, edited 1 time in total.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Array issue

Postby seandisanti » Mon Apr 01, 2013 10:57 am

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.

sweetpeet
New php-forum User
New php-forum User
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: Array issue

Postby sweetpeet » Mon Apr 01, 2013 10:59 am

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.


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
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Array issue

Postby seandisanti » Mon Apr 01, 2013 11:05 am

please show the output you're receiving, and the output you'd like.

sweetpeet
New php-forum User
New php-forum User
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: Array issue

Postby sweetpeet » Mon Apr 01, 2013 11:29 am

seandisanti wrote:please show the output you're receiving, and the output you'd like.


This is what I'm getting (it doesn't seem to be appending any more, which is good): Included: null, , , tomato, lettuce, onion,

This is what I want: Included: null, tomato, lettuce, onion,

And this is the code I have:

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;

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Array issue

Postby seandisanti » Mon Apr 01, 2013 11:35 am

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;
 

sweetpeet
New php-forum User
New php-forum User
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: Array issue

Postby sweetpeet » Mon Apr 01, 2013 11:40 am

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;


PERFECT! thank you!!

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Array issue

Postby seandisanti » Mon Apr 01, 2013 11:45 am

no problem. What i did there, was use a ternary operation to conditionally add either the value if there was one, or nothing if the variable was empty.

(condition that evaluates to true or false)?value if true:value if false;

it's a neat little structure, but have to be careful not to over use them lest your readability really suffers.


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron