Construct Parameter from Array

Hi Chaps (Sorry, I put the wrong subject in, should be: Construct Parameter from Array),

I have a while loop which produces a couple of variables:

do {
$L_NAME.$i = 'Item 1';
$L_NUMBER.$i = '0001';
$L_QTY.$i = '1';
$L_AMT.$i = 10.50;
$NVP.$i = "L_NAME.$i.= .$L_NAME.$i.&L_NUMBER.$i. = .$L_NUMBER.$i.&amp";
} while($i == $items);

Basically what I need to do is contruct a parameter made up of all the $NVP variables.

I’ve a feeling that I would need to add each variable to an array and then use a foreach statement, but I’m not 100% sure.

I’d be interested in the correct way to go about this, any comments welcome!

$arr = array();

$L_NAME0 = 'Item 1';
$L_NUMBER0 = '0001';
$L_QTY0 = '1';
$L_AMT0 = 10.50;
$NVP0 = "L_NAME0 = $L_NAME0&L_NUMBER0 = $L_NUMBER0&amp";

$arr[] = $NVP0;

$L_NAME1 = 'Item 2';
$L_NUMBER1 = '0002';
$L_QTY1 = '3';
$L_AMT1 = 20.50;
$NVP1 = "L_NAME1 = $L_NAME1&L_NUMBER1 = $L_NUMBER1&amp";

$arr[] = $NVP1;

$joined_nvp = join("", $arr);

echo $joined_nvp;

Thread title changed :slight_smile:

so you have a loop that iterates through and you want to build a variable from something inside that loop?

Create a variable OUTSIDE the loop that will be filled by the contents of the loop


$myNewNVP = ''; //empty to start with

do {
    $L_NAME.$i = 'Item 1';
    $L_NUMBER.$i = '0001';
    $L_QTY.$i = '1';
    $L_AMT.$i = 10.50;
    $NVP.$i = "L_NAME.$i.= .$L_NAME.$i.&L_NUMBER.$i. = .$L_NUMBER.$i.&amp";
    $myNewNVP .= "L_NAME.$i.= .$L_NAME.$i.&L_NUMBER.$i. = .$L_NUMBER.$i.&amp"; // .= to append to a variable
} while($i == $items); 

echo $myNewNVP ;

Try this…


do {
$L_NAME.$i = 'Item 1';
$L_NUMBER.$i = '0001';
$L_QTY.$i = '1';
$L_AMT.$i = 10.50;
$NVP.$i = "L_NAME.$i.= .$L_NAME.$i.&L_NUMBER.$i. = .$L_NUMBER.$i.&amp";
$arr[]=$NVP.$i;
} while($i == $items);