1

Let's say I have data string like this...

one=1&two=2&three=3&four=4&two=2

I'm using php foreach to grab the key/value and sort how I need it at this point

foreach($_POST as $key => $value) {
   if ($key == "two") {
    // $result = Take $value and add it to the previous $value .",";
   }
}

The goal I am trying to reach is how do I take the duplicate keys and add the previous value generated in the loop. For example: The solution would be $result = 2,2,

2
  • 4
    If you POSTed that to your server, you'd only see one two, you would not see both values. Commented May 22, 2012 at 20:54
  • Hey to get access to the name structure of any array ie $_POST, or $myarray use print_r and <pre></pre>. <php.net/manual/en/function.print-r.php> Commented May 22, 2012 at 21:24

5 Answers 5

1

If you're POSTing the string in the question to your server, you would only see one value of two, not both. The 2nd one would overwrite the first value.

If you want multiple values for a key, you can make it an array by using [].

one=1&two[]=2&three=3&four=4&two[]=2

Now, $_POST['two'] will be an array (one, three and four will be strings).

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

Comments

1

This will not work. You get only the last value from POST, GET and REQUEST. You need to parse the $_SERVER['QUERY_STRING'], and if you parsed it, you can iterate your array:

foreach(explode('&',$_SERVER['QUERY_STRING']) as $k => $v)
{
 $val = explode('=',$v);
 $result[$val[0]] = isset($result[$val[0]]) ? $result[$val[0]].','.$val[1]:$val[1];
}

Comments

1
//initial data string
$string = "one=1&two=2&three=3&four=4&two=2";

$results = array();

$data = explode('&', $string);

foreach($data as $param) {

    $query = explode('=', $param);

    $key = $query[0];
    $value = $query[1];

    // check to see if the key has been seen before. 
    // if not, store it in an array for now.
    if(!isset($results[$key])){
        $results[$key] = array($value);
    }
    else{
        // the key is a duplicate, store it in the array
        $results[$key][] = $value;
    }

}

// implode the arrays so that they're in the $result = "2,2" format
foreach($data as $key => $value){
    $data[$key] = implode(',', $value);
}

Also its been mentioned, but if this is coming from a server post then you won't get duplicate keys.

3 Comments

Don't you also need to explode on =?
Yes, i there was no question mark in the question so I left it out of the answer, but if you had one in there you should take it out for this to work
No, I meant you need to do list($key, $value) = explode('=', $value). (I was trying to say you need to explode the equal sign. The question mark was because I was asking a question :-P)
1

Bearing in mind Rocket's advice about multiple POSTed values, you could use implode() on any arrays that arrive:

foreach($_POST as $key=>$value)
{
    if(is_array($value))
        $_POST[$key]=implode(',',$value);
}

to get the string value that you seem to be after.

Comments

0

Store them as an array outside the foreach loop.

$keys = [];
foreach($_POST as $key => $value) {
   if ($key == "two") {
     $keys[] = $value;
   }
}
return $keys

2 Comments

You can't have two of the same keys in an array. This code could just be simplified down to $keys = $_POST['two'] (this would be a string, not an array).
fyi In php you don't need to initialize a array outside of a foreach, in order to have access to the array. Just add stuff to the array and return it when you're finished.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.