Avatar of Sam Cohen
Sam CohenFlag for United States of America

asked on 

Adding an Array loop to a php variable

I am trying to add a foreach loop to the variable: "$myValues"

this is my code:

 $vArray = //my array code
 foreach ( $vArray as $values)
   {
  echo $values[1]."<br>".$values[2]."<br><br>";
    }

how can i show the loop by echoing it as $myValues

NOTE: I've already tried i making it into function, then calling it........But it still needs to be echoed as a variable.
PHP

Avatar of undefined
Last Comment
Ray Paseur
ASKER CERTIFIED SOLUTION
Avatar of Rik-Legger
Rik-Legger
Flag of undefined image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Sam Cohen
Sam Cohen
Flag of United States of America image

ASKER

Eureka!!!!!!
Works.. I spent the last few hours trying to figure this out.
Thanks Rik
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Please consider buying this book.  It will really help you get a foundation in PHP.
http://www.sitepoint.com/books/phpmysql4/

Arrays contain elements that each have a key and a value.  The foreach() iterator runs once for each element.  I am not 100% sure I understand your question, but I think this code example might show you how it works.  Test it here:
http://www.laprbass.com/RAY_temp_all4artz.php

Man pages here:
http://us.php.net/manual/en/book.array.php
http://us.php.net/manual/en/function.array.php
http://us.php.net/manual/en/control-structures.foreach.php
<?php // RAY_temp_all4artz.php
error_reporting(E_ALL);


$x = array
( 'abc' => 'ABC'
, 'def' => 'DEF'
, 'xyz' => 'XYZ'
)
;

$out = NULL;
foreach ($x as $thing)
{
    $out .= '<br/>' . $thing;
}

echo $out;

$x = array();
$x[] = 'ABC';
$x[] = 'DEF';
$x[] = 'XYZ';

$out = NULL;
foreach ($x as $thing)
{
    $out .= '<br/>' . $thing;
}

echo $out;

Open in new window

PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo