2

I need some help writing a code that creates an array in the format I needed in. I have this long string of text like this -> "settings=2&options=3&color=3&action=save"...etc Now the next thing I did to make it into an array is the following:

$form_data = explode("&", $form_data);

Ok so far so good...I now have an array like so:

Array
(
[0] => settings=2
[1] => options=3
[2] => color=3
[3] => action=save
)
1

Ok now I need to know how to do two things. First, how can I remove all occurences of "action=save" from the array?

Second, how can I make this array become a key value pair (associative array)? Like "settings=>2"?

3 Answers 3

3

There's a function for that. :)

parse_str('settings=2&options=3&color=3&action=save', $arr);
if (isset($arr['action']) && $arr['action'] == 'save') {
    unset($arr['action']);
}
print_r($arr);

But just for reference, you could do it manually like this:

$str = 'settings=2&options=3&color=3&action=save';
$arr = array();
foreach (explode('&', $str) as $part) {
    list($key, $value) = explode('=', $part, 2);
    if ($key == 'action' && $value == 'save') {
        continue;
    }
    $arr[$key] = $value;
}

This is not quite equivalent to parse_str, since key[] keys wouldn't be parsed correctly. I'd be sufficient for your example though.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the help however I have one question. While trying your code, it seems it does not process duplicates. For example I have "action=2&settings=3&action=2"..etc. As you can see in the string, there is "action=2" two times. But using the code you had before you added the condition to remove them shows an array but with only one instance of the action=save. Is that true? Or is there a way to keep the duplicates?
@Rick Well, how would you want to keep them? You can't keep both as keys. Usually only the last one sticks. If you parse it manually, you can do something else with it: if (isset($arr[$key])) { /* key already exists, do something about it */ }.
ahhhh, so I am guessing there CANNOT be duplicate of same name keys in the array, that is why?
@Rick Yes, keys are unique. Otherwise which value would you get when accessing $arr['action']?
1
$str = 'settings=2&options=3&color=3&action=save&action=foo&bar=save';
parse_str($str, $array);
$key = array_search('save', $array);
if($key == 'action') {
        unset($array['action']);
}

Ideone Link

1 Comment

ok I am not fully understanding..are you just implying I should use parse_str to make the string into an array? But the way I did it, is exactly what I need which is to remove the "&". Also, did you read the rest of my question on how to do the additional two things?
0

This could help on the parsing your array, into key/values

$array; // your array here
$new_array = array();

foreach($array as $key)
{
    $val = explode('=',$key);
    // could also unset($array['action']) if that's a global index you want removed
    // if so, no need to use the if/statement below - 
    // just the setting of the new_array  
    if(($val[0] != 'action' && $val[1] != 'save'))$new_array[] = array($val[0]=>$val[1]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.