开发者

extract elements from php array and assign as variables

开发者 https://www.devze.com 2023-02-23 05:09 出处:网络
I have been working on this for a while and cannot seem to figure it out at all. Any help would be appreciated. here we go.

I have been working on this for a while and cannot seem to figure it out at all. Any help would be appreciated. here we go.

I have an html form that has a text box and a submit button. the text entere开发者_JAVA百科d in the text box is posted to my .php processor form. Once it gets here, I use:

$textdata = $_POST['textdata'];

$input = explode("\n", $textdata);

this takes the data, splits it by line, and stores each line in an array called $input. from here i can echo $input[0] to get the first line and so on. But I need to use this further down in my script and need to assign a variable to the first line, or $input[0]. $input[0] = $line1; does not work. I think I might have to use extract() and a foreach loop? Any help would be greatly appreciated. thanks!


well fo one thing $input array will always be available, or what you can do if i understand correctly is:

$textdata = $_POST['textdata'];

$input = explode("\n", $textdata); //this should have the array of lines assuming
                                   //that $textdata was \n delimited

$line1 = $input[0]; //use $line1 later in code


$line1 = $input[0];
$line2 = $input[1];
$line3 = $input[2];
// etc.

or:

for ($i=0, $inputlen = count($input); $i < $inputlen; $i++) {
    ${'line'.($i+1)} = $input[$i];
}

or simply:

list($line1, $line2, $line3) = $input;


$input[0]. $input[0] = $line1;

I can't tell if the full-stop in that line is a full-stop or concatenation operator.

For concatenation, it should be this way.

$input[0] = $input[0] . $line1;

or even shorter

$input[0] .= $line1;

If you're just wanting to assign $input[0] to $line1 by value, it's

$line1 = $input[0];

You can also assign a reference using

$line1 =& $input[0];

Using the latter, any changes to $line1 will be present in $input[0].

0

精彩评论

暂无评论...
验证码 换一张
取 消