How to redirect a multidimensional array?

I was looking for hi and low for a solution. I have a dynamic multidimensional array that I need to split and urlencode. The number of elements will change, but they will always have the same keys.

$formFields = Array ( 
[0] => Array ( [form_name] => productID [form_value] => 13 ) 
[1] => Array ( [form_name] => campaign [form_value] => email@gmail.com ) 
[2] => Array ( [form_name] => redirect [form_value] => http://example.com ) ) 

Each array has a form name and a form value.

This is what I'm trying to get to:

$desired_results = 
productID => 13
campaign => email@gmail.com
redirect => http://example.com

Every time I try to break them, I get: form_name => productID or something like that.

I try to accept the results and then urlencode them:

productID=13&campaign=email&gmail.com&redirect=http://example.com&
+5
source share
4 answers

This will return values ​​regardless of the name of the keys.

$result = array();

foreach ($formFields as $key => $value)
{
  $tmp = array_values($value);
  $result[$tmp[0]] = $tmp[1];
}
print(http_build_query($result));

foreach , $value. array_values ​​ . [form_name] , [form_value] .

http_build_query urlencoded.

+3

serialize unserialize:

$str = urlencode(serialize($formFields));

:

$formFields = unserialize(urldecode($str));
+16

custom function for keys and values ​​of urlencode array.

function urlencode_array($array){
    $out_array = array();
    foreach($array as $key => $value){
    $out_array[urlencode($key)] = urlencode($value);
    }
return $out_array;
}
-1
source
<!-- Encode entire array here -->

function encode(&$item, $key) {
$item = rawurlencode($item);
}

array_walk_recursive($input_array, 'encode');
-1
source

All Articles