Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Variables made with foreach

Tags:

post

php

I have several variables coming from an array in $POST_['array'] i wish to make some kind of loop for example foreach that makes, for every value in the variable a variable name of it and assigns the value for it.

For example if i have

$POST_['name'];
$POST_['last'];
$POST_['age'];
$POST_['sex'];

I want the loop to create each variable from the array inside the $_POST with the name of the variable like the following:

$name = 'John';
$last = 'Doe';
$age = '32';
$sex = 'male';

NOTE - The array is coming from a serialized jquery string that puts together all the variables and values in a form into one big string.

Is this possible?

like image 442
Luis Alvarado Avatar asked May 16 '11 21:05

Luis Alvarado


People also ask

Can foreach be used in a variable?

The part of the foreach statement enclosed in parenthesis represents a variable and a collection to iterate. PowerShell creates the variable $<item> automatically when the foreach loop runs. Prior to each iteration through the loop, the variable is set to a value in the collection.

How can we store values from foreach loop into an array in PHP?

Your answerDeclare the $items array outside the loop and use $items[] to add items to the array: $items = array(); foreach($group_membership as $username) { $items[] = $username; } print_r($items); Hope it helps!!

Is foreach faster than for PHP?

PHP in TeluguThe 'foreach' is slow in comparison to the 'for' loop. The foreach copies the array over which the iteration needs to be performed.

Can I use foreach on Object in PHP?

PHP provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a foreach statement. By default, all visible properties will be used for the iteration.


2 Answers

You don't need a loop, you want extract:

extract($_POST); // But use caution, see below


Cautions and best practices

As noted in the comments this forces all parameters in the $_POST array into the current symbol space.

In global space

<?php
extract($_GET);
var_dump($_SERVER); // Can be overwritten by the GET param
?>

The code above illustrates the problem as shown in this answer — some pretty dangerous things can be overwritten in the global space.

Inside a function

function myFunc() {
    // (Mostly) empty symbol space! (excluding super globals)
    extract($_POST);
}

Inside a function, as the first line, no harm done.

Important note: You might think since $_SERVER is a super global, that this exploit could happen inside a function as well. However, in my testing, on PHP Version 5.3.4, it is safe inside a function — neither $_SERVER, $_POST, $_GET, $_SESSION, or presumably other superglobals, could be overwritten.

With options

You can also use extract with extract_type options that do not overwrite.

The best option to use, in my opinion, is simply to prefix all variables from extract:

// $_GET = test=1&name=Joe

extract($_GET, EXTR_PREFIX_ALL, "request_get");

echo $request_get_test; // 1
echo $request_get_name; // Joe

That way you don't have the overwrite problem, but you also know you got everything from the array.

Alternate - looping w/ conditional

If you wanted to do this manually (but still dynamically), or wanted to conditionally extract only a few of the variables, you can use variable variables:

foreach ($_POST as $key => $value) {
    if (isset($$key)) continue;

    $$key = $value;
}

(The example condition I've used is an overwrite prevention.)

like image 64
Nicole Avatar answered Sep 28 '22 07:09

Nicole


Try not to use extract() when using $_POST. You may overwrite variables you want which will result in unpredictable behaviour. It is a bad habit to get into and while is dynamic may not be the best solution.

You can do something like this:

foreach($_POST as $key => $value)
{
    switch($key)
    {
        case "name":
            $name = $value;
        break;
        case "last":
            $last = $value;
        break;
    }
}
like image 38
Jake N Avatar answered Sep 28 '22 07:09

Jake N