DragonStyle
DragonStyle

Reputation: 145

Loop through a string and convert to $_POST in PHP

I have this code:

$string = "prItem1,prItem2,prItem3"; // could contains more

$itemarray = explode(',', $string);

            foreach($itemarray as $var)
            {
                //echo $var."<br/>";
            // HERE I want "convert" any found prItem ($var) to a handle
            // that give me something like that: 
            // $prItem1 = $_POST['prItem1'];
            // $prItem2 = $_POST['prItem2'];
            // ...

            }

As I write it in the code comment: Every prItem is an input field and I want to get the $_POST-Vars.

Did someone habe an Idea how I can do this? Or if is there a much better way to to this?

Upvotes: 0

Views: 803

Answers (2)

Poiz
Poiz

Reputation: 7617

You may want to try an Object-based approach like so:

    <?php

        $string     = "prItem1,prItem2,prItem3"; // could contains more
        $itemArray  = explode(',', $string);

        // INSTANTIATE A NEW NATIVE PHP OBJECT TO HOLD YOUR DATA...
        $objItems   = new stdClass();

        // LOOP THROUGH THE $itemArray:
        foreach($itemArray as $var){
            // CREATE INDIVIDUAL KEYS MATCHING UP TO THE DESIRED KEYS AS IN: prItem1, prItem2, etc...
            $key    = $var;

            // ASSIGN THE KEY TO THE OBJECT WHILE SETTING THE VALUE OF THIS OBJECT KEY TO THE POST VALUE...
            $objItems->$key = $_POST[$var];

            // ... MORE LOGIC FOLLOWS...
        }

        // ACCESS YOUR ITEMS LIKE SO...
        //$objItems->prItem1
        var_dump($objItems->prItem1); 

Here is another Variant; this time using an Array instead:

    <?php

        $string     = "prItem1,prItem2,prItem3"; // could contains more
        $itemArray  = explode(',', $string);


        // INSTANTIATE A NEW ARRAY TO HOLD YOUR DATA...
        $arrItems   = array();

        // LOOP THROUGH THE $itemArray:
        foreach($itemArray as $var){
            // CREATE INDIVIDUAL KEYS MATCHING UP TO THE DESIRED KEYS AS IN: prItem1, prItem2, etc...
            $key            = $var;

            // ASSIGN THE KEY TO THE ARRAY WHILE SETTING THE VALUE OF THIS KEY TO THE POST VALUE...
            $arrItems[$key] = $_POST[$var];

            // ... MORE LOGIC FOLLOWS...
        }
        // EXTRACT EACH INDIVIDUAL ARRAY DATA INTO AN OWN VARIABLE
        extract($arrItems);


        // ACCESS YOUR VARIABLES DIRECTLY 
        // LIKE YOU WOULD A PREVIOUSLY DEFINED VARIABLE LIKE SO...
        var_dump($prItem1);

EDIT: SIMULATE AN ARBITRARY POST VALUES - JUST FOR TESTING PURPOSES.

<?php
    $POST       = array("prItem1"=>"Value of prItem1", "prItem2"=>"Value of prItem2", "prItem3"=>"Value of prItem3");
    $string     = "prItem1,prItem2,prItem3"; // could contains more
    $itemArray  = explode(',', $string);


    // INSTANTIATE A NEW ARRAY TO HOLD YOUR DATA...
    $arrItems   = array();

    // LOOP THROUGH THE $itemArray:
    foreach($itemArray as $var){
        // CREATE INDIVIDUAL KEYS MATCHING UP TO THE DESIRED KEYS AS IN: prItem1, prItem2, etc...
        $key            = $var;

        // ASSIGN THE KEY TO THE ARRAY WHILE SETTING THE VALUE OF THIS KEY TO THE POST VALUE...
        $arrItems[$key] = $POST[$var];

        // ... MORE LOGIC FOLLOWS...
    }
    // EXTRACT EACH INDIVIDUAL ARRAY DATA INTO AN OWN VARIABLE
    extract($arrItems);


    // ACCESS YOUR VARIABLES DIRECTLY
    // LIKE YOU WOULD A PREVIOUSLY DEFINED VARIABLE LIKE SO...
    var_dump($prItem1);
    var_dump($prItem2);
    var_dump($prItem3);

I hope this does the trick for you....

Upvotes: 1

user2560539
user2560539

Reputation:

$string = "prItem1,prItem2,prItem3"; // could contains more
$itemarray = explode(',', $string);
$posts = array();
        //remove empty elements from $itemarray
        foreach(array_filter($itemarray) as $var) 
            {
              if(isset($_POST[$var])) {
                   $posts[$var] = $_POST[$var];
                   }
            }
print_r($posts);

Upvotes: 1

Related Questions