0

I am trying to figure out the best way to use dot notation when passing in a key or set of keys into a function and getting that post value.

Example

shipping.first_name

What it looks like in actual $_POST array:

$_POST[shipping][first_name] = 'some value'

I would like to be able to pass in (as a parameter) the string, and have the function return the post value.

function get_post($str = NULL){
    return $_POST[$key1][$key1]..etc.
}

Current attempt (working as intended, but need to put into $_POST):

From: SO Question

function assignArrayByPath(&$arr, $path) {
        $keys = explode('.', $path);

        while ($key = array_shift($keys)) {
            $arr = &$arr[$key];
        }
    }

    $output = array();
    assignArrayByPath($output, $str);

This produces an array of:

Array ( [shipping] => Array ( [first_name] => ) )

I would like then to do something like this:

return isset($_POST.$output) ? true : false;

So how do I take that array created from the period separated string and check if it exists in POST?

I think this might be a duplicate, but I am not positive. I apologize in advance if it is. Any help is much appreciated.

Community
  • 1
  • 1
klye_g
  • 1,242
  • 6
  • 31
  • 54
  • Maybe this can help: http://stackoverflow.com/questions/1432486/creating-object-instance-from-posted-data-php – Babblo Nov 28 '13 at 02:12

1 Answers1

1

See Laravel array_set implement http://laravel.com/api/source-function-array_set.html#319

    /**
     * Set an array item to a given value using "dot" notation.
     *
     * If no key is given to the method, the entire array will be replaced.
     *
     * @param  array   $array
     * @param  string  $key
     * @param  mixed   $value
     * @return array
     */
    function array_set(&$array, $key, $value)
    {
            if (is_null($key)) return $array = $value;

            $keys = explode('.', $key);

            while (count($keys) > 1)
            {
                    $key = array_shift($keys);

                    // If the key doesn't exist at this depth, we will just create an empty array
                    // to hold the next value, allowing us to create the arrays to hold final
                    // values at the correct depth. Then we'll keep digging into the array.
                    if ( ! isset($array[$key]) || ! is_array($array[$key]))
                    {
                            $array[$key] = array();
                    }

                    $array =& $array[$key];
            }

            $array[array_shift($keys)] = $value;

            return $array;
    }

Check exists you can see array_get http://laravel.com/api/source-function-array_get.html#224

    /**
     * Get an item from an array using "dot" notation.
     *
     * @param  array   $array
     * @param  string  $key
     * @param  mixed   $default
     * @return mixed
     */
    function array_get($array, $key, $default = null)
    {
            if (is_null($key)) return $array;

            if (isset($array[$key])) return $array[$key];

            foreach (explode('.', $key) as $segment)
            {
                    if ( ! is_array($array) || ! array_key_exists($segment, $array))
                    {
                            return value($default);
                    }

                    $array = $array[$segment];
            }

            return $array;
    }
Kai
  • 352
  • 1
  • 9
  • I have the string to array portion taken care of. What I need to do now is take that array and return it's value from the $_POST array. So when calling the get_post function it would check isset($_POST.$output). That's the part I am trying to figure out. – klye_g Nov 28 '13 at 02:25