user1424269 user1424269 - 1 month ago 7
PHP Question

PHP Pull Subordinate Info in Get function

I'm a complete newb to PHP - I tried researching this problem, but haven't found an answer. I'm using Formstack to create an order form for a non-profit. At the end of the process, formstack appends all of the entered data to the URL. Typically, I use a simple PHP GET to pull the information I need. In this instance, however, there is a second layer of data after the = of the primary item's name.

Each item shows up similar to this (example items is Chickens):

&chickens=charge_type+%3D+fixed_amount%0Aquantity+%3D+2%0Aunit_price+%3D+15.00%0Atotal+%3D+30&

I need to pull quantity=2, where quantity is a subordinate value of chickens. My knowledge ends at pulling the entire string following chickens=

<?php echo htmlspecialchars($_GET["chickens"]); ?>


Which results in: charge_type = fixed_amount quantity = 2 unit_price = 15.00 total = 30

Any thoughts would be greatly appreciated. Thanks!

Answer

Depending on what you are going to do, you could transform the values in Chicken Array form:

// separate all the value-key pairs in chicken into an array
$array = explode("\n", $_GET["chickens"]);

// init an array that will store all values as associative array
$chickens = array();

foreach($array as $valuepair) {
    // go too the key-value-pairs and split them at the "=" char
    $tmp = explode("=", $valuepair);
    // add value under keyname to array and remove some spaces with trim function 
    $chickens[trim($tmp[0])] = trim($tmp[1]);
}

// show what we got as key-values, as debug output 
print_r($chickens);

// show one value of the array
echo $chickens['quantity'];