Re: assign array values to variables

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



PJ schreef:
> Seems it should be simple, but how does one extract values from an array
> and assign them to a variable?
> foreach ($categoriesIN as $category) {

1. if $categoriesIN comes from a POST, use $_POST['categoriesIN'] instead.
better yet use the filter extension (part of the core) to retrieve the input.

2. validate & santize your input (the reason to use the filter extension)

3. WHY do you want to extract the values and assign them to vars?
since you don't know the number of entries there is no point in creating
a stack of variables of which you have no idea what they are even called
or how many there are. instead loop the array or use an index into the
array to find the data your looking for. you don't have to create a
specific variable to use a piece of data the given element of an array
is just as valid.

4. there is the extract() function ... use at your own risk.

5. do something like this:

foreach ($array as $key => $val)
	$$key = $val;

.. again use at your own risk.

>     echo "$category<br>";
>     }
> or
> if(!empty($_POST['categoriesIN'])){
> foreach( $_POST['categoriesIN'] as $key => $value) {
>     echo "value = $value<br>";
>         }
> both show the values entered in the form but do not create a variable.
> Since the number of entries will vary, I cannot assign a number of
> variable names before knowing how many entries there may be.
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux