Page 1 of 1

Adding values to an array from a textfiled

Posted: Sun Jul 25, 2004 5:32 am
by Calimero
textfiled name='tf1'

I use GET to insert it into the process.php

This is a problem>

If someone enters three or more words into the textfield in this exact format: "word1 word2 word3 word4" - without the quotes off course :)

what is the code to create an array that will contain those (in this case) 4 words - in other words, I need a script to explode(this I know) a textfield and insert each value/word as a new member of an array (array1 or ex)

Already looked at the manual, lost several hours, everithing works but this part of inserting values into an array (or creating a new one with the values inserted) just won't work.

I'm not submitting the code, because its just a few lines of code, and is probably completely useless, some combination of built in functions in php, but nothing more.


Thanks Ahead !

Posted: Sun Jul 25, 2004 6:14 am
by litebearer
I presume you mean something like this...

Code: Select all

<?PHP

$first_array = array ("item1", "item2", "item3");

$textfield = "word1 word2 word3 word4";
$second_array = explode(" ", $textfield);

$count = count($second_array);

echo $count;

$i = 0;

for ($i=0; $i < $count; $i ++) {
  $first_array[] = $second_array[$i];
}
$count2 = count($first_array);
$i = 0;
for ($i = 0; $i < $count2; $i ++) {
  echo $first_array[$i] . "<BR>";
}

?>
of course using the array_merge function is easier

http://us4.php.net/array_merge

Lite...


feyd | switch

Code: Select all

to

Code: Select all

tag.[/color]

...

Posted: Sun Jul 25, 2004 4:55 pm
by Calimero
Any hints on how to use array_merge, I tried, didn't work

Thanks Ahead !

Posted: Sun Jul 25, 2004 5:07 pm
by feyd
example only

Code: Select all

$newarray = array_merge($somearray, $anotherarray&#1111;, ...] );

...

Posted: Mon Jul 26, 2004 3:59 am
by Calimero
I know that syntax, but can it be used to create an array,

NOTE performance is very important here for me - it's a query for adwords and things like that

YIIIIIPEEEEEEEEEEEEEEEE my 100th POST :)))
now I'm good for another 100 :))))

Thanks Ahead !

Posted: Mon Jul 26, 2004 10:38 am
by feyd
[php_man]array_merge[/php_man] creates a new array. That's all it does.